diff --git a/src/Data/Convert/Texmacs/fromtm.cpp b/src/Data/Convert/Texmacs/fromtm.cpp index eb0c02871c..0c0c9eca14 100644 --- a/src/Data/Convert/Texmacs/fromtm.cpp +++ b/src/Data/Convert/Texmacs/fromtm.cpp @@ -76,10 +76,13 @@ tm_reader::skip_blank () { template string tm_reader::decode (string s) { - int i, n= N (s); - string r; - for (i= 0; i < n; i++) - if (((i + 1) < n) && (s[i] == '\\')) { + int start= index_of (s, '\\'); + if (start == -1) return s; + + int n= N (s), n_minus_one= n - 1, i= start; + string r= s (0, start); + for (; i < n_minus_one; i++) + if (s[i] == '\\') { i++; if (s[i] == ';') ; @@ -92,6 +95,11 @@ tm_reader::decode (string s) { else r << s[i]; } else r << s[i]; + // if r[n-2] is not slash, i==n-1 and r[n-1] is not visited + if (i < n) { + r << s[i]; + } + return r; } @@ -407,6 +415,7 @@ is_expand (tree t, string s, int n) { tree texmacs_document_to_tree (string s) { + bench_start ("tmdoc_to_tree_" * as_string (N (s))); tree error (ERROR, "bad format or data"); if (starts (s, "edit") || starts (s, "TeXmacs") || starts (s, "\\(\\)(TeXmacs")) { @@ -448,6 +457,7 @@ texmacs_document_to_tree (string s) { d << A (doc); doc= d; } + bench_end ("tmdoc_to_tree_" * as_string (N (s)), 100); return upgrade (doc, version); } return error; diff --git a/tests/Data/Convert/convert_test.cpp b/tests/Data/Convert/convert_test.cpp index baf8d7fd49..8f5ad7c305 100644 --- a/tests/Data/Convert/convert_test.cpp +++ b/tests/Data/Convert/convert_test.cpp @@ -12,16 +12,23 @@ #include "base.hpp" #include "convert.hpp" +#include "file.hpp" #include "tree_helper.hpp" Q_DECLARE_METATYPE (tree) Q_DECLARE_METATYPE (string) +Q_DECLARE_METATYPE (url) class TestConverter : public QObject { Q_OBJECT private slots: + void initTestCase () { init_lolly (); } void test_search_metadata_data (); void test_search_metadata (); + void test_texmacs_to_tree_data (); + void test_texmacs_to_tree (); + void test_texmacs_to_tree_bench_data (); + void test_texmacs_to_tree_bench (); }; void @@ -69,5 +76,39 @@ TestConverter::test_search_metadata () { qcompare (search_metadata (input_tree, "invalid"), invalid); } +void +TestConverter::test_texmacs_to_tree_data () { + QTest::addColumn ("input_string"); + QTest::addColumn ("output"); + + QTest::newRow ("pure string") << string ("text") << tree (DOCUMENT, "text"); + QTest::newRow ("escaped string") + << string ("tex\\\\t") << tree (DOCUMENT, "tex\\t"); + QTest::newRow ("non escaped trailing slash") + << string ("text\\") << tree (DOCUMENT, "text\\"); +} +void +TestConverter::test_texmacs_to_tree () { + QFETCH (string, input_string); + QFETCH (tree, output); + QCOMPARE (texmacs_to_tree (input_string), output); +} + +void +TestConverter::test_texmacs_to_tree_bench_data () { + QTest::addColumn ("file_name"); + url tm_base ("$TEXMACS_PATH/tests/tm/"); + QTest::newRow ("29_1_1.tm") << string ("$TEXMACS_PATH/tests/tm/29_1_1.tm"); + QTest::newRow ("46_3.tm") << string ("$TEXMACS_PATH/tests/tm/46_3.tm"); + QTest::newRow ("64_1.tm") << string ("$TEXMACS_PATH/tests/tm/64_1.tm"); +} +void +TestConverter::test_texmacs_to_tree_bench () { + QFETCH (string, file_name); + string file_content; + load_string (file_name, file_content, true); + QBENCHMARK { texmacs_to_tree (file_content); }; +} + QTEST_MAIN (TestConverter) #include "convert_test.moc"