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:
56
400_xowa/src/gplx/xowa/apps/utls/Xoa_url_encoder.java
Normal file
56
400_xowa/src/gplx/xowa/apps/utls/Xoa_url_encoder.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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.utls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
public class Xoa_url_encoder { // NOTE: redundant with Gfo_url_encoder, but is simpler; DATE:2016-09-15
|
||||
private final Bry_bfr bfr = Bry_bfr_.New();
|
||||
public byte[] Encode(byte[] src) {
|
||||
int src_len = src.length;
|
||||
boolean dirty = false;
|
||||
for (int i = 0; i < src_len; ++i) {
|
||||
byte b = src[i];
|
||||
byte[] repl = null;
|
||||
switch (b) {
|
||||
case Byte_ascii.Space: repl = Bry__underline; break;
|
||||
case Byte_ascii.Amp: repl = Bry__amp; break;
|
||||
case Byte_ascii.Apos: repl = Bry__apos; break;
|
||||
case Byte_ascii.Eq: repl = Bry__eq; break;
|
||||
case Byte_ascii.Plus: repl = Bry__plus; break;
|
||||
}
|
||||
|
||||
// not a replacement sequence
|
||||
if (repl == null) {
|
||||
// if dirty, add to bfr; else, ignore
|
||||
if (dirty)
|
||||
bfr.Add_byte(b);
|
||||
}
|
||||
else {
|
||||
// if clean, add everything before cur_pos to bfr
|
||||
if (!dirty) {
|
||||
bfr.Add_mid(src, 0, i);
|
||||
dirty = true;
|
||||
}
|
||||
bfr.Add(repl);
|
||||
}
|
||||
}
|
||||
return dirty ? bfr.To_bry_and_clear() : src;
|
||||
}
|
||||
private static final byte[] Bry__amp = Bry_.new_a7("%26"), Bry__eq = Bry_.new_a7("%3D")
|
||||
, Bry__plus = Bry_.new_a7("%2B"), Bry__apos = Bry_.new_a7("%27")
|
||||
, Bry__underline = new byte[] {Byte_ascii.Underline}
|
||||
;
|
||||
}
|
||||
31
400_xowa/src/gplx/xowa/apps/utls/Xoa_url_encoder__tst.java
Normal file
31
400_xowa/src/gplx/xowa/apps/utls/Xoa_url_encoder__tst.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
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.utls; import gplx.*; import gplx.xowa.*; import gplx.xowa.apps.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Xoa_url_encoder__tst {
|
||||
private final Xoa_url_encoder__fxt fxt = new Xoa_url_encoder__fxt();
|
||||
@Test public void Syms__diff() {fxt.Test__encode(" &'=+", "_%26%27%3D%2B");}
|
||||
@Test public void Syms__same() {fxt.Test__encode("!\"#$%()*,-./:;<>?@[\\]^_`{|}~", "!\"#$%()*,-./:;<>?@[\\]^_`{|}~");}
|
||||
@Test public void Mixed() {fxt.Test__encode("a &'=+b", "a_%26%27%3D%2Bb");} // PURPOSE: make sure dirty flag is set
|
||||
}
|
||||
class Xoa_url_encoder__fxt {
|
||||
private final Xoa_url_encoder encoder = new Xoa_url_encoder();
|
||||
public void Test__encode(String raw, String expd) {
|
||||
Gftest.Eq__bry(Bry_.new_u8(expd), encoder.Encode(Bry_.new_u8(raw)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user