1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

v1.11.2.1

This commit is contained in:
gnosygnu
2014-11-09 23:35:57 -05:00
parent 8e372c8cd3
commit da8180ea44
69 changed files with 1072 additions and 566 deletions

View File

@@ -32,72 +32,79 @@ public class Bry_ {
rv[i] = (byte)ary[i];
return rv;
}
public static byte[] new_ascii_(String s) {
public static byte[] new_ascii_(String str) {
try {
if (s == null) return null;
int s_len = s.length();
if (s_len == 0) return Bry_.Empty;
byte[] rv = new byte[s_len];
for (int i = 0; i < s_len; ++i) {
char c = s.charAt(i);
if (str == null) return null;
int str_len = str.length();
if (str_len == 0) return Bry_.Empty;
byte[] rv = new byte[str_len];
for (int i = 0; i < str_len; ++i) {
char c = str.charAt(i);
if (c > 128) c = '?';
rv[i] = (byte)c;
}
return rv;
}
catch (Exception e) {throw Err_.err_(e, "invalid ASCII sequence; s={0}", s);}
catch (Exception e) {throw Err_.err_(e, "invalid ASCII sequence; str={0}", str);}
}
public static byte[] new_ascii_safe_null_(String s) {return s == null ? null : new_ascii_(s);}
public static byte[] new_ascii_lang(String s) {
try {return s.getBytes("ASCII");}
catch (Exception e) {throw Err_.err_(e, "unsupported encoding");}
}
public static byte[] new_utf8_(String s) {
public static int new_utf8_len(String s, int s_len) {
int rv = 0;
for (int i = 0; i < s_len; ++i) {
char c = s.charAt(i);
int c_len = 0;
if ( c < 128) c_len = 1; // 1 << 7
else if ( c < 2048) c_len = 2; // 1 << 11
else if ( (c > 55295) // 0xD800
&& (c < 56320)) c_len = 4; // 0xDFFF
else c_len = 3; // 1 << 16
if (c_len == 4) ++i; // surrogate is 2 wide, not 1
rv += c_len;
}
return rv;
}
public static void new_utf8_write(String str, int str_len, byte[] bry, int bry_pos) {
for (int i = 0; i < str_len; ++i) {
char c = str.charAt(i);
if ( c < 128) {
bry[bry_pos++] = (byte)c;
}
else if ( c < 2048) {
bry[bry_pos++] = (byte)(0xC0 | (c >> 6));
bry[bry_pos++] = (byte)(0x80 | (c & 0x3F));
}
else if ( (c > 55295) // 0xD800
&& (c < 56320)) { // 0xDFFF
if (i >= str_len) throw Err_.new_fmt_("incomplete surrogate pair at end of String; char={0}", c);
char nxt_char = str.charAt(i + 1);
int v = 0x10000 + (c - 0xD800) * 0x400 + (nxt_char - 0xDC00);
bry[bry_pos++] = (byte)(0xF0 | (v >> 18));
bry[bry_pos++] = (byte)(0x80 | (v >> 12) & 0x3F);
bry[bry_pos++] = (byte)(0x80 | (v >> 6) & 0x3F);
bry[bry_pos++] = (byte)(0x80 | (v & 0x3F));
++i;
}
else {
bry[bry_pos++] = (byte)(0xE0 | (c >> 12));
bry[bry_pos++] = (byte)(0x80 | (c >> 6) & 0x3F);
bry[bry_pos++] = (byte)(0x80 | (c & 0x3F));
}
}
}
public static byte[] new_utf8__null(String str) {return str == null ? null : new_utf8_(str);}
public static byte[] new_utf8_(String str) {
try {
int s_len = s.length();
int b_pos = 0;
for (int i = 0; i < s_len; ++i) {
char c = s.charAt(i);
int c_len = 0;
if ( c < 128) c_len = 1; // 1 << 7
else if ( c < 2048) c_len = 2; // 1 << 11
else if ( (c > 55295) // 0xD800
&& (c < 56320)) c_len = 4; // 0xDFFF
else c_len = 3; // 1 << 16
if (c_len == 4) ++i; // surrogate is 2 wide, not 1
b_pos += c_len;
}
byte[] rv = new byte[b_pos];
b_pos = -1;
for (int i = 0; i < s_len; ++i) {
char c = s.charAt(i);
if ( c < 128) {
rv[++b_pos] = (byte)c;
}
else if ( c < 2048) {
rv[++b_pos] = (byte)(0xC0 | (c >> 6));
rv[++b_pos] = (byte)(0x80 | (c & 0x3F));
}
else if ( (c > 55295) // 0xD800
&& (c < 56320)) { // 0xDFFF
if (i >= s_len) throw Err_.new_fmt_("incomplete surrogate pair at end of String; char={0}", c);
char nxt_char = s.charAt(i + 1);
int v = 0x10000 + (c - 0xD800) * 0x400 + (nxt_char - 0xDC00);
rv[++b_pos] = (byte)(0xF0 | (v >> 18));
rv[++b_pos] = (byte)(0x80 | (v >> 12) & 0x3F);
rv[++b_pos] = (byte)(0x80 | (v >> 6) & 0x3F);
rv[++b_pos] = (byte)(0x80 | (v & 0x3F));
++i;
}
else {
rv[++b_pos] = (byte)(0xE0 | (c >> 12));
rv[++b_pos] = (byte)(0x80 | (c >> 6) & 0x3F);
rv[++b_pos] = (byte)(0x80 | (c & 0x3F));
}
}
int str_len = str.length();
int bry_len = new_utf8_len(str, str_len);
byte[] rv = new byte[bry_len];
new_utf8_write(str, str_len, rv, 0);
return rv;
}
catch (Exception e) {throw Err_.err_(e, "invalid UTF-8 sequence; s={0}", s);}
catch (Exception e) {throw Err_.err_(e, "invalid UTF-8 sequence; s={0}", str);}
}
public static byte[] new_utf8_lang(String s) {
try {return s.getBytes("UTF-8");}

View File

@@ -248,7 +248,32 @@ public class Bry_bfr {
Add(val);
return this;
}
public Bry_bfr Add_str(String v) {return Add(Bry_.new_utf8_(v));}
public Bry_bfr Add_str(String v) {return Add_str_utf8(v);}
public Bry_bfr Add_str_utf8(String str) {
try {
int str_len = str.length();
int bry_len = Bry_.new_utf8_len(str, str_len);
if (bfr_len + bry_len > bfr_max) Resize((bfr_max + bry_len) * 2);
Bry_.new_utf8_write(str, str_len, bfr, bfr_len);
bfr_len += bry_len;
return this;
}
catch (Exception e) {throw Err_.err_(e, "invalid UTF-8 sequence; str={0}", str);}
}
public Bry_bfr Add_str_ascii(String str) {
try {
int bry_len = str.length();
if (bfr_len + bry_len > bfr_max) Resize((bfr_max + bry_len) * 2);
for (int i = 0; i < bry_len; ++i) {
char c = str.charAt(i);
if (c > 128) c = '?';
bfr[i + bfr_len] = (byte)c;
}
bfr_len += bry_len;
return this;
}
catch (Exception e) {throw Err_.err_(e, "invalid UTF-8 sequence; str={0}", str);}
}
public Bry_bfr Add_float(float f) {Add_str(Float_.Xto_str(f)); return this;}
public Bry_bfr Add_double(double v) {Add_str(Double_.Xto_str(v)); return this;}
public Bry_bfr Add_dte(DateAdp val) {return Add_dte_segs(val.Year(), val.Month(),val.Day(), val.Hour(), val.Minute(), val.Second(), val.Frac());}