Skip to content

Instantly share code, notes, and snippets.

@rscottm
Created March 15, 2013 04:29
Show Gist options
  • Select an option

  • Save rscottm/5167486 to your computer and use it in GitHub Desktop.

Select an option

Save rscottm/5167486 to your computer and use it in GitHub Desktop.
This is the code I used to read the JCodings tables and create Java code for byte arrays.
def read_int_at(file, index)
(file[index] << 24) | (file[index+1] << 16) | (file[index+2] << 8) | file[index+3]
end
def hex_int_at(file, index, pad_length=0)
rv = read_int_at(file, index).to_s(16)
pad = "0" * (pad_length > rv.length ? (pad_length - rv.length) : 0)
"0x#{pad}#{rv}"
end
#// static final int CaseFold_From[] = readIntArray("CaseFold_From");
def generate_int_array(name, wrap=8, pad=" ")
file = IO.read("#{name}.bin")
puts "#{pad}static final int #{name}[] = {"
a = []
count = read_int_at(file, 0)
1.upto(count) do |i|
a << hex_int_at(file, i*4)
(puts("#{pad}#{pad}#{a.join(', ')}#{i == count ? '' : ','}"); a=[]) if a.length == wrap
end
puts("#{pad}#{pad}#{a.join(', ')}") unless a.empty?
puts "#{pad}};\n\n"
end
# static final int CaseFold_To[][] = readNestedIntArray("CaseFold_To");
def generate_nested_int_array(name, wrap=6, pad=" ")
file = IO.read("#{name}.bin")
puts "#{pad}static final int #{name}[][] = {"
count1 = read_int_at(file, 0)
offset = 4
aa = []
1.upto(count1) do |i|
count2 = read_int_at(file, offset)
offset += 4
a = []
1.upto(count2) do |j|
a << hex_int_at(file, offset)
offset += 4
end
aa << "{#{a.join(', ')}}"
(puts("#{pad}#{pad}#{aa.join(', ')}#{i == count1 ? '' : ','}"); aa=[]) if aa.length == wrap
end
puts("#{pad}#{pad}#{aa.join(', ')}") unless aa.empty?
puts "#{pad}};\n\n"
end
generate_int_array("CaseFold_From")
generate_nested_int_array("CaseFold_To")
generate_int_array("CaseFold_Locale_From")
generate_nested_int_array("CaseFold_Locale_To")
generate_int_array("CaseUnfold_11_From")
generate_nested_int_array("CaseUnfold_11_To")
generate_int_array("CaseUnfold_11_Locale_From")
generate_nested_int_array("CaseUnfold_11_Locale_To")
generate_nested_int_array("CaseUnfold_12")
generate_nested_int_array("CaseUnfold_12_Locale")
generate_nested_int_array("CaseUnfold_13")
@rscottm

rscottm commented Mar 15, 2013

Copy link
Copy Markdown
Author

Run this in the resources/tables directory of jcodings and capture the output to replace the readIntArray and readNestedIntArray calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment