string utils tests pass

This commit is contained in:
Henry Schimke
2022-08-30 10:03:39 -05:00
parent 057a61db18
commit 2b18a51f30
2 changed files with 96 additions and 74 deletions

View File

@@ -23,98 +23,115 @@
// import java.nio.charset.StandardCharsets; // import java.nio.charset.StandardCharsets;
// import java.util.Random; // import java.util.Random;
use rand::{Rng}; use encoding::Encoding;
use encoding::{Encoding}; use rand::Rng;
use std::{collections::HashMap, hash::Hash}; use std::collections::HashMap;
use crate::common::StringUtils; use crate::common::StringUtils;
#[test] #[test]
fn testRandom() { fn test_random() {
let mut r = rand::thread_rng(); let mut r = rand::thread_rng();
let bytes : Vec<u8> = vec![0;1000]; let mut bytes: Vec<u8> = vec![0; 1000];
for i in 0..bytes.len() { for i in 0..bytes.len() {
bytes[i] = r.gen(); bytes[i] = r.gen();
} }
assert_eq!(encoding::all::UTF_8.name(), StringUtils::guessCharset(&bytes, HashMap::new()).name()); assert_eq!(
encoding::all::UTF_8.name(),
StringUtils::guessCharset(&bytes, HashMap::new()).name()
);
} }
#[test] #[test]
fn testShortShiftJIS1() { fn test_short_shift_jis1() {
// 金魚 // 金魚
doTest(&vec![ 0x8b, 0xe0, 0x8b, 0x9b, ], encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), "SJIS"); do_test(
&vec![0x8b, 0xe0, 0x8b, 0x9b],
encoding::label::encoding_from_whatwg_label("SJIS").unwrap(),
"SJIS",
);
} }
#[test] #[test]
fn testShortISO885911() { fn test_short_iso885911() {
// båd // båd
doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, StandardCharsets.ISO_8859_1, "ISO8859_1"); do_test(
&vec![0x62, 0xe5, 0x64],
encoding::all::ISO_8859_1,
"ISO8859_1",
);
} }
#[test] #[test]
fn testShortUTF81() { fn test_short_utf8() {
// Español // Español
doTest(new byte[] { (byte) 0x45, (byte) 0x73, (byte) 0x70, (byte) 0x61, (byte) 0xc3, do_test(
(byte) 0xb1, (byte) 0x6f, (byte) 0x6c }, &vec![0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c],
StandardCharsets.UTF_8, "UTF8"); encoding::all::UTF_8,
"UTF8",
);
} }
#[test] #[test]
fn testMixedShiftJIS1() { fn test_mixed_shift_jis1() {
// Hello 金! // Hello 金!
doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f, do_test(
(byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, }, &vec![0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21],
StringUtils.SHIFT_JIS_CHARSET, "SJIS"); encoding::label::encoding_from_whatwg_label("SJIS").unwrap(),
"SJIS",
);
} }
#[test] #[test]
fn testUTF16BE() { fn test_utf16_be() {
// 调压柜 // 调压柜
doTest(new byte[] { (byte) 0xFE, (byte) 0xFF, (byte) 0x8c, (byte) 0x03, (byte) 0x53, (byte) 0x8b, do_test(
(byte) 0x67, (byte) 0xdc, }, &vec![0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc],
StandardCharsets.UTF_16, encoding::all::UTF_16BE,
StandardCharsets.UTF_16.name()); encoding::all::UTF_16BE.name(),
);
} }
#[test] #[test]
fn testUTF16LE() { fn test_utf16_le() {
// 调压柜 // 调压柜
doTest(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x03, (byte) 0x8c, (byte) 0x8b, (byte) 0x53, do_test(
(byte) 0xdc, (byte) 0x67, }, &vec![0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67],
StandardCharsets.UTF_16, encoding::all::UTF_16LE,
StandardCharsets.UTF_16.name()); encoding::all::UTF_16LE.name(),
);
} }
fn doTest(bytes :&Vec<u8>, charset : &dyn Encoding, encoding: &str) { fn do_test(bytes: &Vec<u8>, charset: &dyn Encoding, encoding: &str) {
let guessedCharset = StringUtils::guessCharset(bytes, HashMap::new()); let guessedCharset = StringUtils::guessCharset(bytes, HashMap::new());
let guessedEncoding = StringUtils::guessEncoding(bytes, HashMap::new()); let guessedEncoding = StringUtils::guessEncoding(bytes, HashMap::new());
assert_eq!(charset.name(), guessedCharset.name()); assert_eq!(charset.name(), guessedCharset.name());
assert_eq!(encoding, guessedEncoding); assert_eq!(encoding, guessedEncoding);
} }
/** // /**
* Utility for printing out a string in given encoding as a Java statement, since it's better // * Utility for printing out a string in given encoding as a Java statement, since it's better
* to write that into the Java source file rather than risk character encoding issues in the // * to write that into the Java source file rather than risk character encoding issues in the
* source file itself. // * source file itself.
* // *
* @param args command line arguments // * @param args command line arguments
*/ // */
fn main(String[] args) { // fn main(String[] args) {
String text = args[0]; // String text = args[0];
Charset charset = Charset.forName(args[1]); // Charset charset = Charset.forName(args[1]);
StringBuilder declaration = new StringBuilder(); // StringBuilder declaration = new StringBuilder();
declaration.append("new byte[] { "); // declaration.append("new byte[] { ");
for (byte b : text.getBytes(charset)) { // for (byte b : text.getBytes(charset)) {
declaration.append("(byte) 0x"); // declaration.append("(byte) 0x");
int value = b & 0xFF; // int value = b & 0xFF;
if (value < 0x10) { // if (value < 0x10) {
declaration.append('0'); // declaration.append('0');
} // }
declaration.append(Integer.toHexString(value)); // declaration.append(Integer.toHexString(value));
declaration.append(", "); // declaration.append(", ");
} // }
declaration.append('}'); // declaration.append('}');
System.out.println(declaration); // System.out.println(declaration);
} // }
// } // }

View File

@@ -121,7 +121,12 @@ impl StringUtils {
if bytes.len() > 2 if bytes.len() > 2
&& ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE)) && ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE))
{ {
if bytes[0] == 0xFE && bytes[1] == 0xFF {
return encoding::all::UTF_16BE; return encoding::all::UTF_16BE;
}else {
return encoding::all::UTF_16LE;
}
} }
// For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS, // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
@@ -148,7 +153,7 @@ impl StringUtils {
// for (int i = 0; // for (int i = 0;
// i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8); // i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
// i++) { // i++) {
if canBeISO88591 || canBeShiftJIS || canBeUTF8 { if !(canBeISO88591 || canBeShiftJIS || canBeUTF8) {
break; break;
} }
@@ -618,7 +623,7 @@ impl BitArray {
* Reverses all bits in the array. * Reverses all bits in the array.
*/ */
pub fn reverse(&mut self) { pub fn reverse(&mut self) {
let mut newBits = Vec::with_capacity(self.bits.len()); let mut newBits = vec![0;self.bits.len()];
// reverse all int's first // reverse all int's first
let len = (self.size - 1) / 32; let len = (self.size - 1) / 32;
let oldBitsLen = len + 1; let oldBitsLen = len + 1;
@@ -848,7 +853,7 @@ impl BitMatrix {
// throw new IllegalArgumentException(); // throw new IllegalArgumentException();
// } // }
let mut bits = Vec::with_capacity(stringRepresentation.len()); let mut bits = vec![false;stringRepresentation.len()];
let mut bitsPos = 0; let mut bitsPos = 0;
let mut rowStartPos = 0; let mut rowStartPos = 0;
let mut rowLength = 0; //-1; let mut rowLength = 0; //-1;
@@ -1132,7 +1137,7 @@ impl BitMatrix {
let mut newWidth = self.height; let mut newWidth = self.height;
let mut newHeight = self.width; let mut newHeight = self.width;
let mut newRowSize = (newWidth + 31) / 32; let mut newRowSize = (newWidth + 31) / 32;
let mut newBits = Vec::with_capacity((newRowSize * newHeight).try_into().unwrap()); let mut newBits = vec![0;(newRowSize * newHeight).try_into().unwrap()];
for y in 0..self.height { for y in 0..self.height {
//for (int y = 0; y < height; y++) { //for (int y = 0; y < height; y++) {