Login
extensions.cr at tip
Login

File src/remilib/extensions.cr from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   274
   275
   276
   277
   278
   279
   280
   281
   282
   283
   284
   285
   286
   287
   288
   289
   290
   291
   292
   293
   294
   295
   296
   297
   298
   299
   300
   301
   302
   303
   304
   305
   306
   307
   308
   309
   310
   311
   312
   313
   314
   315
   316
   317
   318
   319
   320
   321
   322
   323
   324
   325
   326
   327
   328
   329
   330
   331
   332
   333
   334
   335
   336
   337
   338
   339
   340
   341
   342
   343
   344
   345
   346
   347
   348
   349
   350
   351
   352
   353
   354
   355
   356
   357
   358
   359
   360
   361
   362
   363
   364
   365
   366
   367
   368
   369
   370
   371
   372
   373
   374
   375
   376
   377
   378
   379
   380
   381
   382
   383
   384
   385
   386
   387
   388
   389
   390
   391
   392
   393
   394
   395
   396
   397
   398
   399
   400
   401
   402
   403
   404
   405
   406
   407
   408
   409
   410
   411
   412
   413
   414
   415
   416
   417
   418
   419
   420
   421
   422
   423
   424
   425
   426
   427
   428
   429
   430
   431
   432
   433
   434
   435
   436
   437
   438
   439
   440
   441
   442
   443
   444
   445
   446
   447
   448
   449
   450
   451
   452
   453
   454
   455
   456
   457
   458
   459
   460
   461
   462
   463
   464
   465
   466
   467
   468
   469
   470
   471
   472
   473
   474
   475
   476
   477
   478
   479
   480
   481
   482
   483
   484
   485
   486
   487
   488
   489
   490
   491
   492
   493
   494
   495
   496
   497
   498
   499
   500
   501
   502
   503
   504
   505
   506
   507
   508
   509
   510
   511
   512
   513
   514
   515
   516
   517
   518
   519
   520
   521
   522
   523
   524
   525
   526
   527
   528
   529
   530
   531
   532
   533
   534
   535
   536
   537
   538
   539
   540
   541
   542
   543
   544
   545
   546
   547
   548
   549
   550
   551
   552
   553
   554
   555
   556
   557
   558
   559
   560
   561
   562
   563
   564
   565
   566
   567
   568
   569
   570
   571
   572
   573
   574
   575
   576
   577
   578
   579
   580
   581
   582
   583
   584
   585
   586
   587
   588
   589
   590
   591
   592
   593
   594
   595
   596
   597
   598
   599
   600
   601
   602
   603
   604
   605
   606
   607
   608
   609
   610
   611
   612
   613
   614
   615
   616
   617
   618
   619
   620
   621
   622
   623
   624
   625
   626
   627
   628
   629
   630
   631
   632
   633
   634
   635
   636
   637
   638
   639
   640
   641
   642
   643
   644
   645
   646
   647
   648
   649
   650
   651
   652
   653
   654
   655
   656
   657
   658
   659
   660
   661
   662
   663
   664
   665
   666
   667
   668
   669
   670
   671
   672
   673
   674
   675
   676
   677
   678
   679
   680
   681
   682
   683
   684
   685
   686
   687
   688
   689
   690
   691
   692
   693
   694
   695
   696
   697
   698
   699
   700
   701
   702
   703
   704
   705
   706
   707
   708
   709
   710
   711
   712
   713
   714
   715
   716
   717
   718
   719
   720
   721
   722
   723
   724
   725
   726
   727
   728
   729
   730
   731
   732
   733
   734
   735
   736
   737
   738
   739
   740
   741
   742
   743
   744
   745
   746
   747
   748
   749
   750
   751
   752
   753
   754
   755
   756
   757
   758
   759
   760
   761
   762
   763
   764
   765
   766
   767
   768
   769
   770
   771
   772
   773
   774
   775
   776
   777
   778
   779
   780
   781
   782
   783
   784
   785
   786
   787
   788
   789
   790
   791
   792
   793
   794
   795
   796
   797
   798
   799
   800
   801
   802
   803
   804
   805
   806
   807
   808
   809
   810
   811
   812
   813
   814
   815
   816
   817
   818
   819
   820
   821
   822
   823
   824
   825
   826
   827
   828
   829
   830
   831
   832
   833
   834
   835
   836
   837
   838
   839
   840
   841
   842
   843
   844
   845
   846
   847
   848
   849
#### libremiliacr
#### Copyright(C) 2020-2024 Remilia Scarlet <remilia@posteo.jp>
####
#### This program is free software: you can redistribute it and/or modify
#### it under the terms of the GNU General Public License as published
#### the Free Software Foundation, either version 3 of the License, or
#### (at your option) any later version.
####
#### This program is distributed in the hope that it will be useful,
#### but WITHOUT ANY WARRANTY; without even the implied warranty of
#### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
#### GNU General Public License for more details.
####
#### You should have received a copy of the GNU General Public License
#### along with this program.If not, see<http:####www.gnu.org/licenses/.>
require "math"
require "big"

####
#### Extensions to the standard library.
####

# A convenience macro that is the as doing `(thing & flag) != 0`, but slightly
# cleaner looking.
macro bitflag?(thing, flag)
  (({{thing}} & {{flag}}) != 0)
end

struct Time
  UNIVERSAL_TIME_OFFSET = 2_208_988_800

  # Returns the number of seconds since the beginning of the year 1900.
  #
  # [See the Common Lisp documentation on Universal Time](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_u.htm#universal_time)
  def toUniversal : Int64
    self.to_unix + UNIVERSAL_TIME_OFFSET
  end

  # Creates a new `Time` instance from the given universal time.
  #
  # [See the Common Lisp documentation on Universal Time](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_u.htm#universal_time)
  def self.universal(seconds : Int64) : Time
    self.unix(universalToUnix(seconds))
  end

  # Converts a universal time to a Unix time.
  #
  # [See the Common Lisp documentation on Universal Time](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_u.htm#universal_time)
  def self.universalToUnix(sec : Int64) : Int64
    sec - UNIVERSAL_TIME_OFFSET
  end

  # Converts a Unix time to a Universal time.
  #
  # [See the Common Lisp documentation on Universal Time](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_u.htm#universal_time)
  def self.unixToUniversal(sec : Int64) : Int64
    sec + UNIVERSAL_TIME_OFFSET
  end
end

class ::Array(T)
  # Returns a new `Slice` that contains the same elements as this array.
  def toSlice : Slice(T)
    ret = Slice(T).new(self.size)
    self.to_unsafe.copy_to(ret.to_unsafe, self.size)
    ret
  end
end

struct ::Slice(T)
  # Returns a new `Array` that contains the same elements as this slice.
  def toArray : Array(T)
    Array(T).new(self.size) do |i|
      self.[i]
    end
  end

  # Similar to `realloc()` in C.  This returns a new `Slice` with the new
  # requested size.  The contents of `self` are copied over to the new `Slice`.
  # If the new `Slice` is larger than `self`, then the new elements are set to
  # `default`.  If the new `Slice` is smaller, then only as many items as will
  # fit are copied over.
  #
  # Internally, this is equivalent to either using `Slice#[range]` when the new
  # size is smaller, `Slice#dup` when the sizes are the same, and
  # `Slice#initialize` + `Slice#copy_from` when the new size is larger.
  def realloc(newSize : Int32, default : T) : self
    if newSize < self.size
      # Just return a new slice
      self[0...newSize]
    elsif newSize == self.size
      # Copy
      self.dup
    else
      # Larger than self
      ret = Slice(T).new(newSize, default)
      ret.copy_from(self)
      ret
    end
  end
end

abstract class ::IO
  # Temporarily changes the stream position to `gotoHere`, then yields.  This
  # automatically returns to the position before calling this method before it
  # returns.
  #
  # This only works for types that can get and set their position.
  def withExcursion(gotoHere, &)
    oldPos = self.pos
    self.pos = gotoHere
    yield
    self.pos = oldPos
  end

  # Yields, then automatically returns to the position before calling this
  # method before it returns.
  #
  # This only works for types that can get and set their position.
  def withExcursion(&)
    oldPos = self.pos
    yield
    self.pos = oldPos
  end

  # Reads *count* bytes into a `::Bytes` and returns the new `::Bytes`.  If
  # *count* bytes could not be read, this will return a `::Bytes` that is equal
  # in length to the number of bytes actually read.
  def self.readBytes(io : IO, count : Int) : Bytes
    raise ::ArgumentError.new("Bad byte count") if count < 0
    ret = Bytes.new(count)
    numRead = io.read(ret)
    if numRead == count
      ret
    else
      ret[0...numRead]
    end
  end

  # :ditto:
  @[AlwaysInline]
  def readBytes(count : Int) : Bytes
    IO.readBytes(self, count)
  end

  # Reads a 24-bit signed little-endian integer from `io`.
  @[AlwaysInline]
  def self.readInt24(io : IO) : Int32
    b1 = io.read_byte.try &.to_i32! || raise IO::EOFError.new("Could not read the first byte of a 24-bit int")
    b2 = io.read_byte.try &.to_i32! || raise IO::EOFError.new("Could not read the second byte of a 24-bit int")
    b3 = io.read_byte.try &.to_i32! || raise IO::EOFError.new("Could not read the third byte of a 24-bit int")
    ret : Int32 = b3 << 16 | b2 << 8 | b1
    ret |= ~0x7FFFFF if ret & 0x800000 != 0
    ret
  end

  # Reads a 24-bit signed big-endian integer from `io`.
  @[AlwaysInline]
  def self.readInt24BE(io : IO) : Int32
    b3 = io.read_byte.try &.to_i32! || raise IO::EOFError.new("Could not read the first byte of a 24-bit int")
    b2 = io.read_byte.try &.to_i32! || raise IO::EOFError.new("Could not read the second byte of a 24-bit int")
    b1 = io.read_byte.try &.to_i32! || raise IO::EOFError.new("Could not read the third byte of a 24-bit int")
    ret : Int32 = b3 << 16 | b2 << 8 | b1
    ret |= ~0x7FFFFF if ret & 0x800000 != 0
    ret
  end

  # Reads a 24-bit signed little-endian integer from `io`.
  @[AlwaysInline]
  def readInt24
    IO.readInt24(self)
  end

  # Reads a 24-bit signed big-endian integer from `io`.
  @[AlwaysInline]
  def readInt24BE
    IO.readInt24BE(self)
  end

  # Writes `data` as a 24-bit signed little-endian integer.  This does not check
  # to see if `data` is within the range of a 24-bit integer and always writes
  # it as 24-bits.
  @[AlwaysInline]
  def writeInt24(data : Int32)
    write_byte((data & 0x0000FF).to_u8!)
    write_byte(((data & 0x00FF00) >> 8).to_u8!)
    write_byte(((data & 0xFF0000) >> 16).to_u8!)
  end

  # Writes `data` as a 24-bit signed big-endian integer.  This does not check to
  # see if `data` is within the range of a 24-bit integer and always writes it
  # as 24-bits.
  @[AlwaysInline]
  def writeInt24BE(data : Int32)
    write_byte(((data & 0xFF0000) >> 16).to_u8!)
    write_byte(((data & 0x00FF00) >> 8).to_u8!)
    write_byte((data & 0x0000FF).to_u8!)
  end

  ###
  ### Convenience functions.
  ###
  ### These can all be accomplished with the standard library, but these names
  ### are a bit shorter to type.
  ###

  # Convenience method to read a signed 8-bit byte from `io`.  If the byte
  # cannot be read, this will raise an `IO::EOFError`.
  #
  # This is the same as calling `io.read_byte.try &.to_i8! || raise IO::EOFError.new`,
  # just shorter.
  @[AlwaysInline]
  def self.readInt8(io : IO) : Int8
    io.read_byte.try &.to_i8! || raise IO::EOFError.new
  end

  # Convenience method to read a signed 8-bit byte.  If the byte cannot be read,
  # this will raise an `IO::EOFError`.
  #
  # This is the same as calling `io.read_byte.try &.to_i8! || raise IO::EOFError.new`,
  # just shorter.
  @[AlwaysInline]
  def readInt8
    IO.readInt8(self)
  end

  # Convenience method to write a signed 8-bit byte to `io`.
  #
  # This is the same as calling `io.write_byte(value.to_u8!)`, just shorter.
  @[AlwaysInline]
  def self.writeInt8(io : IO, value : Int8) : Nil
    io.write_byte(value.to_u8!)
  end

  # Convenience method to write a signed 8-bit byte.
  #
  # This is the same as calling `io.write_byte(value.to_u8!)`, just shorter.
  @[AlwaysInline]
  def writeInt8(value : Int8) : Nil
    self.write_byte(value.to_u8!)
  end

  # Convenience method to read an unsigned 8-bit byte from `io`.  If the byte
  # cannot be read, this will raise an `IO::EOFError`.
  #
  # This is the same as calling `io.read_byte || raise IO::EOFError.new`, just
  # shorter.
  @[AlwaysInline]
  def self.readUInt8(io : IO) : UInt8
    io.read_byte || raise IO::EOFError.new
  end

  # Convenience method to read an unsigned 8-bit byte.  If the byte cannot be
  # read, this will raise an `IO::EOFError`.
  #
  # This is the same as calling `io.read_byte || raise IO::EOFError.new`, just
  # shorter.
  @[AlwaysInline]
  def readUInt8
    IO.readUInt8(self)
  end

  # Convenience method to write an unsigned 8-bit byte to `io`.
  #
  # This is the same as calling `io.write_byte(value)` and is included for
  # consistency.
  @[AlwaysInline]
  def self.writeUInt8(io : IO, value : UInt8) : Nil
    io.write_byte(value)
  end

  # Convenience method to write an unsigned 8-bit byte.
  #
  # This is the same as calling `io.write_byte(value)` and is included
  # for consistency.
  @[AlwaysInline]
  def writeUInt8(value : UInt8) : Nil
    self.write_byte(value)
  end

  {% begin %}
    {% classes = [Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128] %}
    {% bits = [16, 16, 32, 32, 64, 64, 128, 128] %}
    {% for i in 0...classes.size %}
      # Convenience method to read a signed {{bits[i]}}-bit little-endian integer from `io`.
      #
      # This is the same as calling `io.read_bytes({{classes[i]}}, IO::ByteFormat::LittleEndian)`,
      # just shorter.
      @[AlwaysInline]
      def self.read{{classes[i].id}}(io : IO) : {{classes[i].id}}
        io.read_bytes({{classes[i].id}}, IO::ByteFormat::LittleEndian)
      end

      # Convenience method to read a signed {{bits[i]}}-bit big-endian integer from `io`.
      #
      # This is the same as calling `io.read_bytes({{classes[i].id}}, IO::ByteFormat::BigEndian)`,
      # just shorter.
      @[AlwaysInline]
      def self.read{{classes[i].id}}BE(io : IO) : {{classes[i].id}}
        io.read_bytes({{classes[i].id}}, IO::ByteFormat::BigEndian)
      end

      # Convenience method to read a signed {{bits[i]}}-bit little-endian integer.
      #
      # This is the same as calling `read_bytes({{classes[i].id}}, IO::ByteFormat::LittleEndian)`,
      # just shorter.
      @[AlwaysInline]
      def read{{classes[i].id}}
        IO.read{{classes[i].id}}(self)
      end

      # Convenience method to read a signed {{bits[i]}}-bit big-endian integer.
      #
      # This is the same as calling `read_bytes({{classes[i].id}}, IO::ByteFormat::BigEndian)`,
      # just shorter.
      @[AlwaysInline]
      def read{{classes[i].id}}BE
        IO.read{{classes[i].id}}BE(self)
      end

      # Convenience method to write a signed {{bits[i]}}-bit little-endian integer.  This returns `self.
      #
      # This is the same as calling `write_bytes(value, IO::ByteFormat::LittleEndian)`,
      # just shorter.
      @[AlwaysInline]
      def write{{classes[i].id}}(value : {{classes[i].id}}) : self
        self.write_bytes(value, IO::ByteFormat::LittleEndian)
        self
      end

      # Convenience method to write a signed {{bits[i]}}-bit big-endian integer.  This returns `self.
      #
      # This is the same as calling `write_bytes(value, IO::ByteFormat::BigEndian)`,
      # just shorter.
      @[AlwaysInline]
      def write{{classes[i].id}}BE(value : {{classes[i].id}}) : self
        self.write_bytes(value, IO::ByteFormat::BigEndian)
        self
      end
    {% end %}
  {% end %}

  # Convenience method to write a signed 32-bit little-endian float.  This returns `self.
  #
  # This is the same as calling `write_bytes(value, IO::ByteFormat::LittleEndian)`,
  # just shorter.
  @[AlwaysInline]
  def writeFloat32(value : Float32) : self
    self.write_bytes(value, IO::ByteFormat::LittleEndian)
    self
  end

  # Convenience method to write a signed 64-bit little-endian float.  This returns `self.
  #
  # This is the same as calling `write_bytes(value, IO::ByteFormat::LittleEndian)`,
  # just shorter.
  @[AlwaysInline]
  def writeFloat64(value : Float64) : self
    self.write_bytes(value, IO::ByteFormat::LittleEndian)
    self
  end

  # Convenience method to write a signed 32-bit big-endian float.  This returns `self.
  #
  # This is the same as calling `write_bytes(value, IO::ByteFormat::BigEndian)`,
  # just shorter.
  @[AlwaysInline]
  def writeFloat32BE(value : Float32) : self
    self.write_bytes(value, IO::ByteFormat::BigEndian)
    self
  end

  # Convenience method to write a signed 64-bit big-endian float.  This returns `self.
  #
  # This is the same as calling `write_bytes(value, IO::ByteFormat::BigEndian)`,
  # just shorter.
  @[AlwaysInline]
  def writeFloat64BE(value : Float64) : self
    self.write_bytes(value, IO::ByteFormat::BigEndian)
    self
  end


  # Convenience method to read a 32-bit little-endian floating point number from `io`.
  #
  # This is the same as calling `io.read_bytes(Float32, IO::ByteFormat::LittleEndian)`,
  # just shorter.
  @[AlwaysInline]
  def self.readFloat32(io : IO) : Float32
    io.read_bytes(Float32, IO::ByteFormat::LittleEndian)
  end

  # Convenience method to read a 32-bit big-endian floating point number from `io`.
  #
  # This is the same as calling `io.read_bytes(Float32, IO::ByteFormat::BigEndian)`,
  # just shorter.
  @[AlwaysInline]
  def self.readFloat32BE(io : IO) : Float32
    io.read_bytes(Float32, IO::ByteFormat::BigEndian)
  end

  # Convenience method to read a 32-bit little-endian floating point number.
  #
  # This is the same as calling `io.read_bytes(Float32, IO::ByteFormat::LittleEndian)`,
  # just shorter.
  @[AlwaysInline]
  def readFloat32
    IO.readFloat32(self)
  end

  # Convenience method to read a 32-bit big-endian floating point number.
  #
  # This is the same as calling `io.read_bytes(Float32, IO::ByteFormat::BigEndian)`,
  # just shorter.
  @[AlwaysInline]
  def readFloat32BE
    IO.readFloat32BE(self)
  end

  # Convenience method to read a 64-bit little-endian floating point number from `io`.
  #
  # This is the same as calling `io.read_bytes(Float64, IO::ByteFormat::LittleEndian)`,
  # just shorter.
  @[AlwaysInline]
  def self.readFloat64(io : IO) : Float64
    io.read_bytes(Float64, IO::ByteFormat::LittleEndian)
  end

  # Convenience method to read a 64-bit big-endian floating point number from `io`.
  #
  # This is the same as calling `io.read_bytes(Float64, IO::ByteFormat::BigEndian)`,
  # just shorter.
  @[AlwaysInline]
  def self.readFloat64BE(io : IO) : Float64
    io.read_bytes(Float64, IO::ByteFormat::LittleEndian)
  end

  # Convenience method to read a 64-bit little-endian floating point number.
  #
  # This is the same as calling `io.read_bytes(Float64, IO::ByteFormat::LittleEndian)`,
  # just shorter.
  @[AlwaysInline]
  def readFloat64
    IO.readFloat64(self)
  end

  # Convenience method to read a 64-bit big-endian floating point number.
  #
  # This is the same as calling `io.read_bytes(Float64, IO::ByteFormat::BigEndian)`,
  # just shorter.
  @[AlwaysInline]
  def readFloat64BE
    IO.readFloat64BE(self)
  end
end

module RemiLib
  # :nodoc:
  PRETTY_SIZE_SUFFIXES = [
    "Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
  ]

  PRETTY_SIZE_SUFFIXES_SHORT = [
    "B", "K", "M", "G", "T", "P", "E", "Z", "Y"
  ]
end

struct ::Int
  # Similar to `Int#humanize_bytes`, except that this always uses
  # `Int::BinaryPrefixFormat::JEDEC` for the format, and formats numbers
  # slightly differently.
  #
  # If `alwaysShowAsBytes` is true, then this will always show the size as
  # bytes.
  #
  # `separator` is used for the decimal separator.  `delimiter` is used only
  # when `alwaysShowAsBytes` is true, and is used as the thousands delimiter.
  #
  # `padding` instances of `padChar` will be inserted to the left of the
  # formatted size if `padding` is positive.  If it's negative, then that many
  # instances of `padChar` will be inserted to the right of the formatted size.
  def prettySize(*, alwaysShowAsBytes : Bool = false, decimalPlaces : Int = 2, separator : Char = '.',
                 delimiter : Char = ',', padding : Int = 0, padChar : Char = ' ', shortSuffix : Bool = false) : String
    String.build do |str|
      self.prettySize(str, alwaysShowAsBytes: alwaysShowAsBytes, decimalPlaces: decimalPlaces, separator: separator,
                      delimiter: delimiter, padding: padding, padChar: padChar,
                      shortSuffix: shortSuffix)
    end
  end

  def prettySize(io : IO, *, alwaysShowAsBytes : Bool = false, decimalPlaces : Int = 2, separator : Char = '.',
                 delimiter : Char = ',', padding : Int = 0, padChar : Char = ' ', shortSuffix : Bool = false) : Nil
    if padding > 0
      padding.times { |_| io << padChar }
    end

    if alwaysShowAsBytes
      self.format(io, separator, delimiter, only_significant: false)
      io << (shortSuffix ? " B" : " Bytes")
    else
      mag : Int128 = if self > 0
                       Math.log(self, 1024).to_i128!
                     elsif self < 0
                       Math.log(self.abs, 1024).to_i128!
                     else
                       0i128
                     end
      humanSize : Float64 = if self > 0
                              self.to_i128! / (1u128 << (mag * 10))
                            elsif self < 0
                              -(self.abs.to_i128! / (1u128 << (mag * 10)))
                            else
                              0.0f64
                            end

      if mag == 0
        self.format(io, separator, delimiter, only_significant: false)
        io << (shortSuffix ? " B" : " Bytes")
      elsif decimalPlaces == 0
        humanSize.to_i.format(io, separator, delimiter, only_significant: true)
        io << ' ' << (shortSuffix ? RemiLib::PRETTY_SIZE_SUFFIXES_SHORT[mag] :
                                    RemiLib::PRETTY_SIZE_SUFFIXES[mag])
      else
        humanSize.format(io, separator, delimiter, decimalPlaces, only_significant: false)
        io << ' ' << (shortSuffix ? RemiLib::PRETTY_SIZE_SUFFIXES_SHORT[mag] :
                                    RemiLib::PRETTY_SIZE_SUFFIXES[mag])
      end
    end

    if padding < 0
      padding.abs.times { |_| io << padChar }
    end
  end

  ROMAN_NUMERALS = [
    {1000, "M"},
    {900, "CM"},
    {500, "D"},
    {400, "CD"},
    {100, "C"},
    {90, "XC"},
    {50, "L"},
    {40, "XL"},
    {10, "X"},
    {9, "IX"},
    {5, "V"},
    {4, "IV"},
    {1, "I"}]

  ENGLISH_NUMBERS = {
    0 => "zero",
    1 => "one",
    2 => "two",
    3 => "three",
    4 => "four",
    5 => "five",
    6 => "six",
    7 => "seven",
    8 => "eight",
    9 => "nine",
    10 => "ten",
    11 => "eleven",
    12 => "twelve",
    13 => "thirteen",
    14 => "fourteen",
    15 => "fifteen",
    16 => "sixteen",
    17 => "seventeen",
    18 => "eighteen",
    19 => "nineteen",
    20 => "twenty",
    30 => "thirty",
    40 => "forty",
    50 => "fifty",
    60 => "sixty",
    70 => "seventy",
    80 => "eighty",
    90 => "ninety"
  }.to_h

  # Converts this number to a Roman numeral.  Only values between 1
  # and 3999, inclusive, are supported, otherwise an `ArgumentError`
  # is raised.
  def toRoman : String
    unless self > 0 && self < 4000
      raise ArgumentError.new("Cannot represent number as a roman numeral: #{self}")
    end

    self.toRoman? || raise "Unexpected nil from #toRoman?"
  end

  # Converts this number to a Roman numeral.  Only values between 1
  # and 3999, inclusive, are supported, otherwise this returns `nil`.
  def toRoman? : String?
    # Adapted from https://github.com/jkfurtney/clformat
    unless self > 0 && self < 4000
      return nil
    end

    i = self
    String.build do |str|
      ROMAN_NUMERALS.each do |num|
        count = i // num[0]
        str << num[1] * count
        i -= num[0] * count
      end
    end
  end

  # :nodoc:
  ONE_DUODECILLION = BigInt.new("1000000000000000000000000000000000000000")

  # :nodoc:
  ONE_TREDECILLION = BigInt.new("1000000000000000000000000000000000000000000")

  # Converts a number into a string such that it appears as English words.  For
  # example, 100 would return `"one hundred"`.
  @[AlwaysInline]
  def positiveSpokenNumber : String
    Int.positiveSpokenNumber(self)
  end

  # :ditto:
  def self.positiveSpokenNumber(num) : String
    # Adapted from https://github.com/jkfurtney/clformat

    # Try to early out
    return ENGLISH_NUMBERS[num] if ENGLISH_NUMBERS[num]?

    raise "ENGLISH_NUMBERS check failed" if num <= 20

    rem = 0
    String.build do |str|
      {% begin %}
        case
        when num < 100
          str << positiveSpokenNumber((num // 10) * 10) << '-' << positiveSpokenNumber(num % 10)

          {% if @type != Int8 && @type != UInt8 %}
          when num < 1000
            str << positiveSpokenNumber(num // 100) << ' ' << "hundred "
            rem = num % 100

          when num < 1000000
            str << positiveSpokenNumber(num // 1000) << ' ' << "thousand"
            rem = num % 1000

          when num < 1000000000
            str << positiveSpokenNumber(num // 1000000) << ' ' << "million"
            rem = num % 1000000

          when num < 1000000000000
            str << positiveSpokenNumber(num // 1000000000) << ' ' << "billion"
            rem = num % 1000000000

            {% if @type != Int16 && @type != UInt16 %}
            when num < 1000000000000000
              str << positiveSpokenNumber(num // 1000000000000) << ' ' << "trillion"
              rem = num % 1000000000000

            when num < 1000000000000000000
              str << positiveSpokenNumber(num // 1000000000000000) << ' ' << "quadrillion"
              rem = num % 1000000000000000

            when num < 1000000000000000000000_i128
              str << positiveSpokenNumber(num // 1000000000000000000_i128) << ' ' << "quintillion"
              rem = num % 1000000000000000000_i128

              {% if @type != Int32 && @type != UInt32 %}
              when num < 1000000000000000000000000_i128
                str << positiveSpokenNumber(num // 1000000000000000000000_i128) << ' ' << "sextillion"
                rem = num % 1000000000000000000000_i128

              when num < 1000000000000000000000000000_i128
                str << positiveSpokenNumber(num // 1000000000000000000000000_i128) << ' ' << "septillion"
                rem = num % 1000000000000000000000000_i128

              when num < 1000000000000000000000000000000_i128
                str << positiveSpokenNumber(num // 1000000000000000000000000000_i128) << ' ' << "octillion"
                rem = num % 1000000000000000000000000000_i128

              when num < 1000000000000000000000000000000000_i128
                str << positiveSpokenNumber(num // 1000000000000000000000000000000_i128) << ' ' << "nonillion"
                rem = num % 1000000000000000000000000000000_i128

              when num < 1000000000000000000000000000000000000_i128
                str << positiveSpokenNumber(num // 1000000000000000000000000000000000_i128) << ' ' << "decillion"
                rem = num % 1000000000000000000000000000000000_i128

                {% if @type != Int64 && @type != UInt64 %}
                when num < ONE_DUODECILLION
                  str << positiveSpokenNumber(num // 1000000000000000000000000000000000000_i128) << ' ' << "undecillion"
                  rem = num % 1000000000000000000000000000000000000_i128

                  {% if BigFloat.has_method?(:to_i128) %}
                  when num < ONE_TREDECILLION
                    str << positiveSpokenNumber(num // ONE_DUODECILLION) << ' ' << "duodecillion"
                    rem = num % ONE_DUODECILLION
                  {% end %}

                {% end %}
              {% end %}
            {% end %}
          {% end %}
        else
          raise ArgumentError.new("Number cannot be represented as an English number")
        end
      {% end %}

      unless rem == 0
        str << (rem >= 100 ? ", " : "") << positiveSpokenNumber(rem)
      end
    end
  end

  def toSpoken : String
    {% begin %}
      {% if @type != UInt8 && @type != UInt16 && @type != UInt32 && @type != UInt64 && @type != UInt128 %}
        if self < 0
          return "minus #{Int.positiveSpokenNumber(-self)}"
        end
      {% end %}
    {% end %}

    Int.positiveSpokenNumber(self)
  end
end

{% begin %}
  {% classes = [::Int8, ::UInt8, ::Int16, ::UInt16, ::Int32, ::UInt32, ::Int64, ::UInt64, ::Int128, ::UInt128] %}
  {% bitSizes = [8, 8, 16, 16, 32, 32, 64, 64, 128, 128] %}
  {% for i in 0...classes.size %}
    struct {{classes[i]}}
      # Returns the number of zero bits in the number before a 1 bit is encountered.
      def numLeadingZeros : Int
        total = 0
        {{bitSizes[i] - 1}}.downto(0) do |i|
          if self.bit(i) == 0
            total += 1
          else
            break
          end
        end

        total
      end

      # Returns the number of one bits in the number before a 0 bit is encountered.
      def numLeadingOnes : Int
        total = 0
        {{bitSizes[i] - 1}}.downto(0) do |i|
          if self.bit(i) == 1
            total += 1
          else
            break
          end
        end

        total
      end
    end
  {% end %}
{% end %}

{% begin %}
  {% classes = [::Int8, ::UInt8, ::Int16, ::UInt16, ::Int32, ::UInt32, ::Int64, ::UInt64, ::Int128, ::UInt128] %}
  {% maskSuffixes = [:_u8, :_u8, :_u16, :_u16, :_u32, :_u32, :_u64, :_u64, :_u128, :_u128] %}
  {% retClasses = [Union(::Int8, ::UInt8), Union(::Int16, ::UInt16), Union(::Int32, ::UInt32),
                   Union(::Int64, ::UInt64), Union(::Int128, ::UInt128)] %}
  {% bitSizes = [8, 8, 16, 16, 32, 32, 64, 64, 128, 128] %}
  {% for i in 0...classes.size %}
    struct {{classes[i]}}
      # Converts this integer to a number that is `bits` bits wide.  If
      # `asUnsigned` is `true`, then the converted value is converted into an
      # unsigned integer of `bits` size, otherwise it is converted to a
      # twos-complement signed integer of `bits` size.
      #
      # `bits` must be at least 1.  If `bits` is greater than or equal to the
      # size of `self` in bits, then `self` is simply returned (possibly
      # converted to an unsigned version if `asUnsigned` is `true`).
      def asBitSize(bits : Int, asUnsigned : Bool = false) : {{retClasses[i // 2]}}
        # Check the requested bit size.
        if bits <= 0
          raise ArgumentError.new("Bad bit size for an integer of #{ {{bitSizes[i]}} } bits")
        elsif bits >= {{bitSizes[i]}}
          if asUnsigned
            return self.to_u{{bitSizes[i]}}!
          else
            return self.to_i{{bitSizes[i]}}!
          end
        end

        # (logand x (logior x (- (mask-field (byte 1 (1- bit-size)) x))))
        mask = ((2{{maskSuffixes[i].id}} << (bits - 1)) - 1)
        ret = self.to_u{{bitSizes[i]}}! & mask

        if asUnsigned
          ret
        else
          if ret.bit(bits - 1) == 0
            ret.to_i{{bitSizes[i]}}
          else
            retMask = -{{bitSizes[i]}}.to_u{{bitSizes[i]}}! & ~mask
            (ret | retMask).to_i{{bitSizes[i]}}!
          end
        end
      end
    end
  {% end %}
{% end %}

class File
  # Creates a file at *path* that is *size* bytes large, filled entirely with
  # zeros.
  def self.createEmpty(path : String|Path, size : Int) : Nil
    File.open(path, "wb") do |file|
      if size > 0
        file.seek((size - 1).to_i64, File::Seek::Set)
        file.write_byte(0)
        file.flush
      end
    end
  end
end

{% begin %}
  {% if compare_versions(Crystal::VERSION, "1.7.0") < 0 %}
    {% puts "Monkey patching byte_swap into the integer classes" %}

    {% classes = [::Int16, ::UInt16, ::Int32, ::UInt32, ::Int64, ::UInt64, ::Int128, ::UInt128] %}
    {% sizes = [2, 2, 4, 4, 8, 8, 16, 16] %}
    {% for klass in 0...classes.size %}
      struct {{classes[klass]}}
        # Swaps the bytes of self; a little-endian value becomes a big-endian
        # value, and vice-versa. The bit order within each byte is unchanged.
        def byte_swap : self
          {% for i in 0...sizes[klass] %}
            b{{i}} = (self & {{0xFF << (8 * i)}}) >> {{8 * i}}
          {% end %}

          {% for i in 0...sizes[klass] %}
            (b{{i}} << {{8 * ((sizes[klass] - 1) - i)}}) |
          {% end %}
          0
        end
      end
    {% end %}
  {% end %}
{% end %}