mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
'v3.9.4.1'
This commit is contained in:
@@ -18,8 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.xowa.apps.gfs; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.langs.phps.*;
|
||||
public class Xoa_gfs_php_mgr {
|
||||
public static byte[] Xto_php(Bry_bfr bfr, boolean escape_backslash, byte[] src) {
|
||||
public class Gfs_php_converter {
|
||||
public static byte[] Xto_php(Bry_bfr bfr, boolean escape_backslash, byte[] src) {
|
||||
int len = src.length;
|
||||
int pos = 0;
|
||||
boolean dirty = false;
|
||||
@@ -87,26 +87,54 @@ public class Xoa_gfs_php_mgr {
|
||||
}
|
||||
}
|
||||
}
|
||||
public static byte[] Xto_gfs(Bry_bfr bfr, byte[] raw) {
|
||||
public static byte[] To_gfs(Bry_bfr bfr, byte[] raw) {
|
||||
int raw_len = raw.length;
|
||||
for (int i = 0; i < raw_len; i++) {
|
||||
for (int i = 0; i < raw_len; ++i) {
|
||||
byte b = raw[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Backslash:
|
||||
case Byte_ascii.Backslash: // unescape; EX: '\"' -> '"'
|
||||
++i;
|
||||
if (i < raw_len)
|
||||
bfr.Add_byte(raw[i]);
|
||||
else
|
||||
if (i < raw_len){
|
||||
byte escape_byte = raw[i];
|
||||
switch (escape_byte) { // REF: http://php.net/manual/en/language.types.String.php
|
||||
case Byte_ascii.Ltr_t: escape_byte = Byte_ascii.Tab; break;
|
||||
case Byte_ascii.Ltr_n: escape_byte = Byte_ascii.Nl; break;
|
||||
case Byte_ascii.Ltr_r: escape_byte = Byte_ascii.Cr; break;
|
||||
case Byte_ascii.Ltr_v: escape_byte = 11; break; // 11=vertical tab
|
||||
case Byte_ascii.Ltr_e: escape_byte = 27; break; // 27=escape
|
||||
case Byte_ascii.Ltr_f: escape_byte = 12; break; // 12=form fed
|
||||
case Byte_ascii.Backslash:
|
||||
case Byte_ascii.Quote:
|
||||
case Byte_ascii.Apos:
|
||||
case Byte_ascii.Dollar: break;
|
||||
// FUTURE:
|
||||
// //\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000")
|
||||
// \ x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation
|
||||
// \ u{[0-9A-Fa-f]+} the sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the String as that codepoint's UTF-8 representation (added in PHP 7.0.0)
|
||||
default: // all else seems to be rendered literally; EX:"You do not need to put \ before a /."; PAGE:en.w:MediaWiki:Spam-whitelist; DATE:2016-09-12
|
||||
bfr.Add_byte(Byte_ascii.Backslash);
|
||||
bfr.Add_byte(escape_byte);
|
||||
continue;
|
||||
}
|
||||
bfr.Add_byte(escape_byte);
|
||||
}
|
||||
else // if eos, just output "\"; don't fail; EX: "a\" -> "a\"
|
||||
bfr.Add_byte(Byte_ascii.Backslash);
|
||||
break;
|
||||
case Byte_ascii.Tilde:
|
||||
case Byte_ascii.Tilde: // double up tilde; EX: '~' -> '~~'
|
||||
bfr.Add_byte_repeat(Bry_fmtr.char_escape, 2); // escape tilde; EX: ~u -> ~~u; DATE:2013-11-11
|
||||
break;
|
||||
case Byte_ascii.Dollar:
|
||||
int end_pos = Php_text_itm_parser.Find_fwd_non_int(raw, i + 1, raw_len);
|
||||
int int_val = Bry_.To_int_or(raw, i + 1, end_pos, -1);
|
||||
bfr.Add_byte(Bry_fmtr.char_escape).Add_byte(Bry_fmtr.char_arg_bgn).Add_int_variable(int_val - 1).Add_byte(Bry_fmtr.char_arg_end);
|
||||
i = end_pos - 1;
|
||||
case Byte_ascii.Dollar: // convert php args to gfs args; EX: $1 -> ~{0}
|
||||
int int_bgn = i + 1;
|
||||
int int_end = Php_text_itm_parser.Find_fwd_non_int(raw, int_bgn, raw_len);
|
||||
if (int_bgn == int_end ) // no numbers after $; EX: "$ "; "$a"
|
||||
bfr.Add_byte(b);
|
||||
else {
|
||||
int int_val = Bry_.To_int_or(raw, int_bgn, int_end, -1);
|
||||
if (int_val == -1) throw Err_.new_wo_type(String_.Format("unknown php dollar sequence: raw=~{0}", raw));
|
||||
bfr.Add_byte(Bry_fmtr.char_escape).Add_byte(Bry_fmtr.char_arg_bgn).Add_int_variable(int_val - List_adp_.Base1).Add_byte(Bry_fmtr.char_arg_end); // convert "$1" -> "~{0}"
|
||||
i = int_end - 1; // -1 b/c Find_fwd_non_int positions after non-int
|
||||
}
|
||||
break;
|
||||
default:
|
||||
bfr.Add_byte(b);
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.apps.gfs; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Gfs_php_converter__to_gfs__tst {
|
||||
@Before public void init() {fxt.Clear();} private final Gfs_php_converter_fxt fxt = new Gfs_php_converter_fxt();
|
||||
@Test public void Escape_sequences() {
|
||||
fxt.Test__to_gfs("a\\\"b" , "a\"b");
|
||||
fxt.Test__to_gfs("a\\'b" , "a'b");
|
||||
fxt.Test__to_gfs("a\\$b" , "a$b");
|
||||
fxt.Test__to_gfs("a\\\\b" , "a\\b");
|
||||
fxt.Test__to_gfs("a\\nb" , "a\nb");
|
||||
fxt.Test__to_gfs("a\\tb" , "a\tb");
|
||||
fxt.Test__to_gfs("a\\rb" , "a\rb");
|
||||
fxt.Test__to_gfs("a\\ b" , "a\\ b"); // "\ " seems to be rendered literally; EX:"You do not need to put \ before a /."; PAGE:en.w:MediaWiki:Spam-whitelist; DATE:2016-09-12
|
||||
fxt.Test__to_gfs("a\\\\b\\'c\\\"d\\$e" , "a\\b'c\"d$e"); // backslash.escape
|
||||
fxt.Test__to_gfs("\\" , "\\"); // backslash.eos; eos, but nothing to escape; render self but dont fail
|
||||
}
|
||||
@Test public void Tilde() {
|
||||
fxt.Test__to_gfs("a~b" , "a~~b"); // tilde.escape
|
||||
}
|
||||
@Test public void Arguments() {
|
||||
fxt.Test__to_gfs("a$1b" , "a~{0}b"); // dollar
|
||||
fxt.Test__to_gfs("a $ b" , "a $ b"); // noop
|
||||
}
|
||||
}
|
||||
class Gfs_php_converter_fxt {
|
||||
private final Bry_bfr bfr = Bry_bfr_.New();
|
||||
public void Clear() {}
|
||||
public void Test__to_gfs(String raw, String expd) {
|
||||
byte[] actl = Gfs_php_converter.To_gfs(bfr, Bry_.new_u8(raw));
|
||||
Tfds.Eq(expd, String_.new_u8(actl));
|
||||
}
|
||||
public void Test_Xto_php_escape_y(String raw, String expd) {Test_Xto_php(raw, Bool_.Y, expd);}
|
||||
public void Test_Xto_php_escape_n(String raw, String expd) {Test_Xto_php(raw, Bool_.N, expd);}
|
||||
public void Test_Xto_php(String raw, boolean escape_backslash, String expd) {
|
||||
byte[] actl = Gfs_php_converter.Xto_php(bfr, escape_backslash, Bry_.new_u8(raw));
|
||||
Tfds.Eq(expd, String_.new_u8(actl));
|
||||
}
|
||||
}
|
||||
@@ -17,14 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.apps.gfs; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*;
|
||||
public class Xoa_gfs_php_mgr_tst {
|
||||
@Before public void init() {fxt.Clear();} private Xoa_gfs_php_mgr_fxt fxt = new Xoa_gfs_php_mgr_fxt();
|
||||
@Test public void Xto_gfs() {
|
||||
fxt.Test_Xto_gfs("a\\\\b\\'c\\\"d\\$e" , "a\\b'c\"d$e"); // backslash.escape
|
||||
fxt.Test_Xto_gfs("\\" , "\\"); // backslash.eos; eos, but nothing to escape; render self but dont fail
|
||||
fxt.Test_Xto_gfs("a~b" , "a~~b"); // tilde.escape
|
||||
fxt.Test_Xto_gfs("a$1b" , "a~{0}b"); // dollar
|
||||
}
|
||||
public class Gfs_php_converter__to_php__tst {
|
||||
@Before public void init() {fxt.Clear();} private final Gfs_php_converter_fxt fxt = new Gfs_php_converter_fxt();
|
||||
@Test public void Xto_php() {
|
||||
fxt.Test_Xto_php_escape_y("a~{0}b" , "a$1b"); // tilde.arg.one
|
||||
fxt.Test_Xto_php_escape_y("a~{0}b~{1}c~{2}d" , "a$1b$2c$3d"); // tilde.arg.many
|
||||
@@ -34,17 +28,3 @@ public class Xoa_gfs_php_mgr_tst {
|
||||
fxt.Test_Xto_php_escape_n("a\\b'c\"d$e" , "a\\b'c\"d$e"); // backslash.escape_n
|
||||
}
|
||||
}
|
||||
class Xoa_gfs_php_mgr_fxt {
|
||||
private Bry_bfr bfr = Bry_bfr_.New();
|
||||
public void Clear() {}
|
||||
public void Test_Xto_gfs(String raw, String expd) {
|
||||
byte[] actl = Xoa_gfs_php_mgr.Xto_gfs(bfr, Bry_.new_u8(raw));
|
||||
Tfds.Eq(expd, String_.new_u8(actl));
|
||||
}
|
||||
public void Test_Xto_php_escape_y(String raw, String expd) {Test_Xto_php(raw, Bool_.Y, expd);}
|
||||
public void Test_Xto_php_escape_n(String raw, String expd) {Test_Xto_php(raw, Bool_.N, expd);}
|
||||
public void Test_Xto_php(String raw, boolean escape_backslash, String expd) {
|
||||
byte[] actl = Xoa_gfs_php_mgr.Xto_php(bfr, escape_backslash, Bry_.new_u8(raw));
|
||||
Tfds.Eq(expd, String_.new_u8(actl));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user