diff --git a/src/tl/tl/tlInclude.cc b/src/tl/tl/tlInclude.cc index 53a5a6dc1..e247ab9f3 100644 --- a/src/tl/tl/tlInclude.cc +++ b/src/tl/tl/tlInclude.cc @@ -134,7 +134,11 @@ IncludeExpander::to_string () const tl_assert (m_sections.begin ()->second.second == 0); std::string fn = m_sections.begin ()->second.first; - return tl::to_word_or_quoted_string (fn, valid_fn_chars); + if (! fn.empty () && fn.front () == '@') { + return tl::to_quoted_string (fn); + } else { + return fn; + } } else { @@ -166,7 +170,9 @@ IncludeExpander::from_string (const std::string &s) ex.read_quoted (ie.m_sections [1].first); - } else if (ex.test ("@")) { + } else if (*ex == '@') { + + ++ex; while (! ex.at_end ()) { diff --git a/src/tl/unit_tests/tlIncludeTests.cc b/src/tl/unit_tests/tlIncludeTests.cc index da681c43e..8ed1651fd 100644 --- a/src/tl/unit_tests/tlIncludeTests.cc +++ b/src/tl/unit_tests/tlIncludeTests.cc @@ -108,3 +108,31 @@ TEST(4_multi_include_interpolate) EXPECT_EQ (ie.translate_to_original (6).second, 3); } +TEST(5_issue946) +{ + std::string fn = tl::testdata () + "/tl/x_inc4.txt"; + + std::string et; + tl::IncludeExpander ie = tl::IncludeExpander::expand (fn, tl::InputStream (fn).read_all (), et); + EXPECT_EQ (et, "A line\nincluded.4\nAnother line\n"); + + EXPECT_EQ (np (ie.to_string ()), np ("@1*" + tl::testdata () + "/tl/x_inc4.txt*0;2*'" + tl::testdata () + "/tl/inc 4.txt'*-1;3*" + tl::testdata () + "/tl/x_inc4.txt*0;")); + EXPECT_EQ (tl::IncludeExpander::from_string (ie.to_string ()).to_string (), ie.to_string ()); + + EXPECT_EQ (ie.translate_to_original (1).first, fn); + EXPECT_EQ (ie.translate_to_original (1).second, 1); + EXPECT_EQ (np (ie.translate_to_original (2).first), np (tl::testdata () + "/tl/inc 4.txt")); + EXPECT_EQ (ie.translate_to_original (2).second, 1); + EXPECT_EQ (ie.translate_to_original (3).first, fn); + EXPECT_EQ (ie.translate_to_original (3).second, 3); + + fn = tl::testdata () + "/tl/inc 4.txt"; + + et.clear (); + ie = tl::IncludeExpander::expand (fn, tl::InputStream (fn).read_all (), et); + EXPECT_EQ (et, "included.4\n"); + + // no quotes here so this string can be used as the original file name if there is no include + EXPECT_EQ (np (ie.to_string ()), np (tl::testdata () + "/tl/inc 4.txt")); +} + diff --git a/testdata/tl/inc 4.txt b/testdata/tl/inc 4.txt new file mode 100644 index 000000000..710f3fd3d --- /dev/null +++ b/testdata/tl/inc 4.txt @@ -0,0 +1 @@ +included.4 diff --git a/testdata/tl/x_inc4.txt b/testdata/tl/x_inc4.txt new file mode 100644 index 000000000..954d8ff6f --- /dev/null +++ b/testdata/tl/x_inc4.txt @@ -0,0 +1,3 @@ +A line +# %include 'inc 4.txt' +Another line