Hash :
24697b12
Author :
Thomas de Grivel
Date :
2023-04-11T23:15:23
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
## c3
## Copyright 2022,2023 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software granted the above
## copyright notice and this permission paragraph are included in all
## copies and substantial portions of this software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
def one(out, su, bits, base_prefix, negative, digits, result)
r = if result != 0 then "#{negative}#{result}" else "#{result}" end
if r == "-9223372036854775808"
# XXX bug in clang integer litterals
r = "-9223372036854775807 - 1"
end
if su == "U"
r = "#{r}U"
end
output = Proc.new do |digits|
zeros = 0
spaces = ""
digits.each do |digit|
if digit != 0 || zeros >= digits.count - 1
break
end
zeros += 1
end
i = zeros
while i >= 0
test_digits = digits[i..(digits.count - 1)]
test_digits = test_digits.join
spaces = " " * (i + ((result.zero? && negative == "-") ? 1 : 0))
out.puts " BUF_PARSE_TEST_#{su}(#{bits}, \"#{negative}#{base_prefix}#{test_digits}\", #{spaces}#{r});"
i -= 1
end
end
output.(digits)
string_digits = false
digits_upcase = digits.map do |digit|
if digit.is_a?(String)
string_digits = true
digit.upcase
else
digit
end
end
output.(digits_upcase) if string_digits
end
def count(out, su, bits, base, base_prefix, negative, max)
result = 0
base.each do |i|
i = 0 if i == "0"
base.each do |j|
j = 0 if j == "0"
base.each do |k|
one(out, su, bits, base_prefix, negative, [i, j, k], result)
result += 1
if result > max
return result
end
end
end
end
return result
end
def number_to_base(m, base)
digits = []
radix = base.count
while m > 0
digits.push(base[m % radix])
m = m / radix
end
digits.reverse
end
def integer_max(su, bits, negative)
if negative == "-"
2 ** (if su == "S" then bits - 1; else bits; end)
else
2 ** (if su == "S" then bits - 1; else bits; end) - 1
end
end
BITS = [8, 16, 32, 64]
licence = File.read("../licence.h")
["S", "U"].each do |su|
BITS.each do |bits|
su_downcase = su.downcase
filename = "buf_parse_test_#{su_downcase}#{bits}.c"
filename_tmp = "#{filename}.tmp"
File.open(filename_tmp, "w") do |out|
out.puts licence
out.puts '#include "buf_parse_test.h"'
[[[0, 1], "0b", "binary"],
[[0, 1, 2, 3, 4, 5, 6, 7], "0o", "octal"],
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"],
"0x", "hexadecimal"],
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "", "decimal"]].each do |b|
base = b[0]
base_prefix = b[1]
base_name = b[2]
radix = base.count
signs = if (su == "S")
["", "-"]
else
[""]
end
signs.each do |negative|
function_name = "buf_parse_test_#{su_downcase}#{bits}_#{base_name}"
if negative == "-"
function_name += "_negative"
end
out.puts ''
out.puts "void #{function_name} ()"
out.puts '{'
max = integer_max(su, bits, negative)
result = count(out, su, bits, base, base_prefix, negative,
max)
BITS.each do |b|
if b <= bits
max = integer_max(su, b, negative)
if result < max - 1
digits = number_to_base(max - 1, base)
one(out, su, bits, base_prefix, negative, digits, max - 1)
end
if result < max
digits = number_to_base(max, base)
one(out, su, bits, base_prefix, negative, digits, max)
end
end
end
out.puts "}"
end
end
end # File.open
cmd = "if ! cmp #{filename} #{filename_tmp}; then mv #{filename_tmp} #{filename}; echo #{filename}; else rm #{filename_tmp}; fi"
raise "command failed" unless system(cmd)
end
end