1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
/*
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.xtns.pfuncs.numbers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.pfuncs.*;
import gplx.core.btries.*; import gplx.intl.*; import gplx.xowa.langs.numbers.*;
public class Pf_formatnum extends Pf_func_base {
@Override public int Id() {return Xol_kwd_grp_.Id_str_formatnum;}
@Override public Pf_func New(int id, byte[] name) {return new Pf_formatnum().Name_(name);}
@Override public boolean Func_require_colon_arg() {return true;}
@Override public void Func_evaluate(Xop_ctx ctx, byte[] src, Xot_invk caller, Xot_invk self, Bry_bfr bfr) {
Xol_lang lang = ctx.Wiki().Lang();
int self_args_len = self.Args_len();
byte[] argx = Eval_argx(ctx, src, caller, self);
byte[] arg1 = Pf_func_.Eval_arg_or_empty(ctx, src, caller, self, self_args_len, 0);
bfr.Add(Format_num(lang, argx, arg1));
}
public static byte[] Format_num(Xol_lang lang, byte[] num, byte[] arg1) {
Btrie_slim_mgr trie_raw = lang.Kwd_mgr().Trie_raw();
Btrie_slim_mgr trie_nosep = lang.Kwd_mgr().Trie_nosep();
int arg1_len = arg1.length;
if (Bry_.Len_gt_0(arg1)) { // argument specified
if (trie_raw .Match_exact(arg1, 0, arg1_len) != null)
return lang.Num_mgr().Raw(num);
else if (trie_nosep .Match_exact(arg1, 0, arg1_len) != null)
return lang.Num_mgr().Format_num_no_separators(num);
}
return lang.Num_mgr().Format_num(num);
}
}

View File

@@ -0,0 +1,38 @@
/*
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.xtns.pfuncs.numbers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.pfuncs.*;
import org.junit.*;
import gplx.intl.*; import gplx.xowa.langs.numbers.*;
public class Pf_formatnum_de_tst {
private Xop_fxt fxt = new Xop_fxt();
@Before public void init() {
fxt.Reset();
fxt.Init_lang_numbers_separators(".", ",");
}
@After public void term() {
fxt.Init_lang_numbers_separators_en();
}
@Test public void Fmt__plain() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234,56}}" , "{{test}}" , "1.234.56");} // NOTE: double "." looks strange, but matches MW; DATE:2013-10-24
@Test public void Fmt__grp_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:1.234,56}}" , "{{test}}" , "1,234.56");}
@Test public void Fmt__dec_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234.56}}" , "{{test}}" , "1.234,56");} // NOTE: "." should be treated as decimal separator, but replaced with ","; DATE:2013-10-21
@Test public void Raw__grp_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:1.234,56|R}}" , "{{test}}" , "1234.56");}
@Test public void Raw__plain() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234,56|R}}" , "{{test}}" , "1234.56");}
@Test public void Raw__dec_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:12,34|R}}" , "{{test}}" , "12.34");} // NOTE: dec_dlm is always ".
@Test public void Nosep__plain() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234,56|NOSEP}}" , "{{test}}" , "1234,56");}
@Test public void Nosep__grp_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:1.234,56|NOSEP}}" , "{{test}}" , "1.234,56");}
}

View File

@@ -0,0 +1,47 @@
/*
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.xtns.pfuncs.numbers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.pfuncs.*;
import org.junit.*;
import gplx.intl.*;
public class Pf_formatnum_en_tst {
private Xop_fxt fxt = new Xop_fxt();
@Before public void init() {fxt.Reset();}
@Test public void Len_4() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234}}" , "{{test}}" , "1,234");}
@Test public void Len_7() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234567}}" , "{{test}}" , "1,234,567");}
@Test public void Len_2() {fxt.Test_parse_tmpl_str_test("{{formatnum:12}}" , "{{test}}" , "12");}
@Test public void Len_10() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234567890}}" , "{{test}}" , "1,234,567,890");}
@Test public void Neg() {fxt.Test_parse_tmpl_str_test("{{formatnum:-1234}}" , "{{test}}" , "-1,234");}
@Test public void Decimal() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234.5678}}" , "{{test}}" , "1,234.5678");}
@Test public void Mixed() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234abc5678}}" , "{{test}}" , "1,234abc5,678");}
@Test public void Zeros() {fxt.Test_parse_tmpl_str_test("{{formatnum:0000000}}" , "{{test}}" , "0,000,000");}
@Test public void Raw__grp_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:1,234.56|R}}" , "{{test}}" , "1234.56");}
@Test public void Raw__plain() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234.56|R}}" , "{{test}}" , "1234.56");}
@Test public void Nosep__plain() {fxt.Test_parse_tmpl_str_test("{{formatnum:1234.56|NOSEP}}" , "{{test}}" , "1234.56");}
@Test public void Nosep__grp_dlm() {fxt.Test_parse_tmpl_str_test("{{formatnum:1,234.56|NOSEP}}" , "{{test}}" , "1,234.56");}
@Test public void Cs() {fxt.Test_parse_tmpl_str_test("{{FORMATNUM:1234}}" , "{{test}}" , "1,234");}
@Test public void Exc_ws() { // PURPOSE: EX: {{rnd|122835.3|(-3)}}
fxt.Test_parse_tmpl_str_test(String_.Concat_lines_nl_skip_last
( "{{formatnum:"
, " {{#expr:2}}"
, "}}"
)
, "{{test}}"
, "2"
);
}
}

View File

@@ -0,0 +1,38 @@
/*
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.xtns.pfuncs.numbers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.pfuncs.*;
import org.junit.*;
import gplx.intl.*; import gplx.xowa.langs.numbers.*;
public class Pf_formatnum_es_tst {
private Xop_fxt fxt;
@Before public void init() {
Xoae_app app = Xoa_app_fxt.app_();
Xol_lang lang = new Xol_lang(app.Lang_mgr(), Bry_.new_a7("es")).Init_by_load_assert();
Xowe_wiki wiki = Xoa_app_fxt.wiki_(app, "es.wikipedia.org", lang);
fxt = new Xop_fxt(app, wiki);
}
@Test public void Basic() {
fxt.Test_parse_tmpl_str_test("{{formatnum:1234.56}}" , "{{test}}", "1234.56"); // fmt.n;
fxt.Test_parse_tmpl_str_test("{{formatnum:1234}}" , "{{test}}", "1234"); // fmt.n; decimal
fxt.Test_parse_tmpl_str_test("{{formatnum:-1234.56}}" , "{{test}}", "-1234.56"); // fmt.n; neg
fxt.Test_parse_tmpl_str_test("{{formatnum:12345.90}}" , "{{test}}", "12,345.90"); // fmt.y; 5
fxt.Test_parse_tmpl_str_test("{{formatnum:123456.90}}" , "{{test}}", "123,456.90"); // fmt.y; 6
fxt.Test_parse_tmpl_str_test("{{formatnum:1234.}}" , "{{test}}", "1,234."); // stress; decimal at end
fxt.Test_parse_tmpl_str_test("{{formatnum:123456a}}" , "{{test}}", "123,456a"); // stress; letters
}
}

View File

@@ -0,0 +1,53 @@
/*
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.xtns.pfuncs.numbers; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.pfuncs.*;
import org.junit.*;
import gplx.intl.*; import gplx.xowa.langs.numbers.*;
public class Pf_formatnum_fa_tst {
private Xop_fxt fxt;
@Before public void init() {
Xoae_app app = Xoa_app_fxt.app_();
Xol_lang lang = new Xol_lang(app.Lang_mgr(), Bry_.new_a7("fa")).Init_by_load_assert();
String gfs = String_.Concat_lines_nl
( "numbers {"
, " digits {"
, " clear;"
, " set('0', '۰');"
, " set('1', '۱');"
, " set('2', '۲');"
, " set('3', '۳');"
, " set('4', '۴');"
, " set('5', '۵');"
, " set('6', '۶');"
, " set('7', '۷');"
, " set('8', '۸');"
, " set('9', '۹');"
, " set('%', '٪');"
, " set('.', '٫');"
, " set(',', '٬');"
, " }"
, "}"
);
app.Gfs_mgr().Run_str_for(lang, gfs);
Xowe_wiki wiki = Xoa_app_fxt.wiki_(app, "fa.wikipedia.org", lang);
fxt = new Xop_fxt(app, wiki);
}
@Test public void Basic() {
fxt.Test_parse_tmpl_str_test("{{formatnum:۱۵۰|R}}" , "{{test}}", "150");
}
}