Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r83122 - in trunk/tools/quickbook: src test/unit
From: dnljms_at_[hidden]
Date: 2013-02-24 06:23:02


Author: danieljames
Date: 2013-02-24 06:23:02 EST (Sun, 24 Feb 2013)
New Revision: 83122
URL: http://svn.boost.org/trac/boost/changeset/83122

Log:
Fix mapped position when breaking up indented code.
Text files modified:
   trunk/tools/quickbook/src/files.cpp | 42 +++++++++++++++++++++++++++++++++------
   trunk/tools/quickbook/test/unit/source_map_test.cpp | 37 +++++++++++++++++++++++++++++++++-
   2 files changed, 70 insertions(+), 9 deletions(-)

Modified: trunk/tools/quickbook/src/files.cpp
==============================================================================
--- trunk/tools/quickbook/src/files.cpp (original)
+++ trunk/tools/quickbook/src/files.cpp 2013-02-24 06:23:02 EST (Sun, 24 Feb 2013)
@@ -296,14 +296,42 @@
                 case mapped_file_section::empty:
                     return section->original_pos;
                 case mapped_file_section::indented: {
- // Indented doesn't really work, but that's okay because we
- // currently don't break up indented code.
- unsigned newlines = std::count(
- this->source().begin() + section->our_pos,
- this->source().begin() + pos, '\n');
+ boost::string_ref::size_type our_line = section->our_pos;
+ unsigned newline_count = 0;
 
- return pos - section->our_pos + section->original_pos +
- newlines * section->indentation;
+ for(boost::string_ref::size_type i = section->our_pos;
+ i != pos; ++i)
+ {
+ if (source()[i] == '\n') {
+ our_line = i + 1;
+ ++newline_count;
+ }
+ }
+
+ if (newline_count == 0)
+ return pos - section->our_pos + section->original_pos;
+
+ boost::string_ref::size_type original_line =
+ section->original_pos;
+
+ while(newline_count > 0) {
+ if (original->source()[original_line] == '\n')
+ --newline_count;
+ ++original_line;
+ }
+
+ for(unsigned i = section->indentation; i > 0; --i) {
+ if (original->source()[original_line] == '\n' ||
+ original->source()[original_line] == '\0') break;
+ assert(original->source()[original_line] == ' ' ||
+ original->source()[original_line] == '\t');
+ ++original_line;
+ }
+
+ assert(original->source()[original_line] ==
+ source()[our_line]);
+
+ return original_line + (pos - our_line);
                 }
                 default:
                     assert(false);

Modified: trunk/tools/quickbook/test/unit/source_map_test.cpp
==============================================================================
--- trunk/tools/quickbook/test/unit/source_map_test.cpp (original)
+++ trunk/tools/quickbook/test/unit/source_map_test.cpp 2013-02-24 06:23:02 EST (Sun, 24 Feb 2013)
@@ -183,7 +183,7 @@
             quickbook::file_position(2,4));
         // TODO: Shouldn't this be (3,1)? Does it matter?
         BOOST_TEST_EQ(f1->position_of(f1->source().end()),
- quickbook::file_position(3,4));
+ quickbook::file_position(3,1));
     }
 
     {
@@ -209,7 +209,7 @@
         BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 11),
             quickbook::file_position(2,4));
         BOOST_TEST_EQ(f1->position_of(f1->source().end()),
- quickbook::file_position(3,4));
+ quickbook::file_position(3,1));
     }
 
     {
@@ -235,9 +235,42 @@
     }
 }
 
+void indented_map_tests2()
+{
+ boost::string_ref source(
+ " Code line1\n"
+ "\n"
+ " Code line2\n");
+ quickbook::file_ptr fake_file = new quickbook::file(
+ "(fake file)", source, 105u);
+
+ quickbook::mapped_file_builder builder;
+
+ {
+ builder.start(fake_file);
+ builder.unindent_and_add(fake_file->source());
+ quickbook::file_ptr f1 = builder.release();
+ BOOST_TEST_EQ(f1->source(),
+ boost::string_ref("Code line1\n\nCode line2\n"));
+ BOOST_TEST_EQ(f1->position_of(f1->source().begin()),
+ quickbook::file_position(1,4));
+ BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 1),
+ quickbook::file_position(1,5));
+ BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 5),
+ quickbook::file_position(1,9));
+ BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 10),
+ quickbook::file_position(1,14));
+ BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 11),
+ quickbook::file_position(2,1));
+ BOOST_TEST_EQ(f1->position_of(f1->source().begin() + 12),
+ quickbook::file_position(3,4));
+ }
+}
+
 int main()
 {
     simple_map_tests();
     indented_map_tests();
+ indented_map_tests2();
     return boost::report_errors();
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk