diff --git a/src/Jamfile.v2 b/src/Jamfile.v2 index 0c55c7a..3bffd0c 100644 --- a/src/Jamfile.v2 +++ b/src/Jamfile.v2 @@ -29,7 +29,6 @@ exe quickbook state.cpp utils.cpp files.cpp - string_ref.cpp input_path.cpp values.cpp id_manager.cpp diff --git a/src/actions.cpp b/src/actions.cpp index 304663f..adaf29d 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -63,7 +63,7 @@ namespace quickbook } std::string add_anchor(quickbook::state& state, - std::string const& id, + boost::string_ref id, id_category::categories category = id_category::explicit_anchor_id) { @@ -474,7 +474,7 @@ namespace quickbook if (saved_conditional) { - string_ref macro1 = values.consume().get_quickbook(); + boost::string_ref macro1 = values.consume().get_quickbook(); std::string macro(macro1.begin(), macro1.end()); state.conditional = find(state.macro, macro.c_str()); @@ -702,7 +702,7 @@ namespace quickbook int code_tag = code_block.get_tag(); value_consumer values = code_block; - string_ref code_value = values.consume().get_quickbook(); + boost::string_ref code_value = values.consume().get_quickbook(); values.finish(); bool inline_code = code_tag == code_tags::inline_code || @@ -710,7 +710,7 @@ namespace quickbook bool block = code_tag != code_tags::inline_code; std::string source_mode = state.source_mode_next.empty() ? - state.source_mode : state.source_mode_next.get_quickbook(); + state.source_mode : detail::to_s(state.source_mode_next.get_quickbook()); state.source_mode_next = value(); if (inline_code) { @@ -726,17 +726,17 @@ namespace quickbook // preprocess the code section to remove the initial indentation mapped_file_builder mapped; mapped.start(state.current_file); - mapped.unindent_and_add(code_value.begin(), code_value.end()); + mapped.unindent_and_add(code_value); file_ptr f = mapped.release(); - if (f->source.empty()) + if (f->source().empty()) return; // Nothing left to do here. The program is empty. if (qbk_version_n >= 107u) state.start_callouts(); - parse_iterator first_(f->source.begin()); - parse_iterator last_(f->source.end()); + parse_iterator first_(f->source().begin()); + parse_iterator last_(f->source().end()); file_ptr saved_file = f; boost::swap(state.current_file, saved_file); @@ -813,8 +813,8 @@ namespace quickbook detail::print_string(v.get_encoded(), out); } else { - std::string value = v.get_quickbook(); - for(std::string::const_iterator + boost::string_ref value = v.get_quickbook(); + for(boost::string_ref::const_iterator first = value.begin(), last = value.end(); first != last; ++first) { @@ -841,8 +841,10 @@ namespace quickbook value_consumer pair = pair_; value name = pair.consume(); value value = pair.consume(); + std::string name_str(name.get_quickbook().begin(), + name.get_quickbook().end()); pair.finish(); - if(!attributes.insert(std::make_pair(name.get_quickbook(), value)).second) + if(!attributes.insert(std::make_pair(name_str, value)).second) { detail::outwarn(name.get_file(), name.get_position()) << "Duplicate image attribute: " @@ -860,7 +862,7 @@ namespace quickbook std::string fileref = attributes["fileref"].is_encoded() ? attributes["fileref"].get_encoded() : - attributes["fileref"].get_quickbook(); + detail::to_s(attributes["fileref"].get_quickbook()); // Check for windows paths, then convert. // A bit crude, but there you go. @@ -1006,7 +1008,7 @@ namespace quickbook void macro_definition_action(quickbook::state& state, quickbook::value macro_definition) { value_consumer values = macro_definition; - std::string macro_id = values.consume().get_quickbook(); + std::string macro_id = detail::to_s(values.consume().get_quickbook()); value phrase_value = values.optional_consume(); std::string phrase; if (phrase_value.check()) phrase = phrase_value.get_encoded(); @@ -1035,11 +1037,11 @@ namespace quickbook void template_body_action(quickbook::state& state, quickbook::value template_definition) { value_consumer values = template_definition; - std::string identifier = values.consume().get_quickbook(); + std::string identifier = detail::to_s(values.consume().get_quickbook()); std::vector template_values; BOOST_FOREACH(value const& p, values.consume()) { - template_values.push_back(p.get_quickbook()); + template_values.push_back(detail::to_s(p.get_quickbook())); } BOOST_ASSERT(values.check(template_tags::block) || values.check(template_tags::phrase)); @@ -1207,7 +1209,7 @@ namespace quickbook file_ptr saved_current_file = state.current_file; state.current_file = content.get_file(); - string_ref source = content.get_quickbook(); + boost::string_ref source = content.get_quickbook(); parse_iterator first(source.begin()); parse_iterator last(source.end()); @@ -1363,7 +1365,7 @@ namespace quickbook bool template_escape = values.check(template_tags::escape); if(template_escape) values.consume(); - std::string identifier = values.consume(template_tags::identifier).get_quickbook(); + std::string identifier = detail::to_s(values.consume(template_tags::identifier).get_quickbook()); std::vector args; @@ -1480,7 +1482,7 @@ namespace quickbook // Note: dst is never actually encoded as boostbook, which // is why the result is called with 'print_string' later. std::string dst = dst_value.is_encoded() ? - dst_value.get_encoded() : dst_value.get_quickbook(); + dst_value.get_encoded() : detail::to_s(dst_value.get_quickbook()); state.phrase << markup.pre; detail::print_string(dst, state.phrase.get()); @@ -1499,7 +1501,7 @@ namespace quickbook write_anchors(state, state.out); value_consumer values = variable_list; - std::string title = values.consume(table_tags::title).get_quickbook(); + std::string title = detail::to_s(values.consume(table_tags::title).get_quickbook()); state.out << "\n"; @@ -1538,7 +1540,7 @@ namespace quickbook std::string element_id; if(values.check(general_tags::element_id)) - element_id = values.consume().get_quickbook(); + element_id = detail::to_s(values.consume().get_quickbook()); value title = values.consume(table_tags::title); bool has_title = !title.empty(); @@ -1785,7 +1787,7 @@ namespace quickbook // Counter-intuitively: encoded == plain text here. std::string path_text = qbk_version_n >= 106u || path.is_encoded() ? - path.get_encoded() : path.get_quickbook(); + path.get_encoded() : detail::to_s(path.get_quickbook()); if(path_text.find('\\') != std::string::npos) { diff --git a/src/code_snippet.cpp b/src/code_snippet.cpp index 4c63a3b..242f23f 100644 --- a/src/code_snippet.cpp +++ b/src/code_snippet.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include "block_tags.hpp" #include "template_stack.hpp" #include "actions.hpp" @@ -30,7 +29,7 @@ namespace quickbook code_snippet_actions(std::vector& storage, file_ptr source_file, char const* source_type) - : last_code_pos(source_file->source.begin()) + : last_code_pos(source_file->source().begin()) , in_code(false) , snippet_stack() , storage(storage) @@ -63,13 +62,13 @@ namespace quickbook std::string id; bool start_code; - std::string::const_iterator source_pos; + string_iterator source_pos; mapped_file_builder::pos start_pos; boost::shared_ptr next; }; void push_snippet_data(std::string const& id, - std::string::const_iterator pos) + string_iterator pos) { boost::shared_ptr new_snippet(new snippet_data(id)); new_snippet->next = snippet_stack; @@ -88,8 +87,8 @@ namespace quickbook } mapped_file_builder content; - std::string::const_iterator mark_begin, mark_end; - std::string::const_iterator last_code_pos; + boost::string_ref::const_iterator mark_begin, mark_end; + boost::string_ref::const_iterator last_code_pos; bool in_code; boost::shared_ptr snippet_stack; std::vector& storage; @@ -352,8 +351,8 @@ namespace quickbook bool is_python = extension == ".py"; code_snippet_actions a(storage, load(filename, qbk_version_n), is_python ? "[python]" : "[c++]"); - string_iterator first(a.source_file->source.begin()); - string_iterator last(a.source_file->source.end()); + string_iterator first(a.source_file->source().begin()); + string_iterator last(a.source_file->source().end()); cl::parse_info info; @@ -383,7 +382,7 @@ namespace quickbook in_code = true; } - content.add(last_code_pos, first); + content.add(boost::string_ref(last_code_pos, first - last_code_pos)); } } @@ -420,7 +419,7 @@ namespace quickbook in_code = true; } - content.add(mark_begin, mark_end); + content.add(boost::string_ref(mark_begin, mark_end - mark_begin)); } void code_snippet_actions::escaped_comment(string_iterator first, string_iterator last) @@ -438,7 +437,7 @@ namespace quickbook snippet_data& snippet = *snippet_stack; content.add("\n", mark_begin); - content.unindent_and_add(mark_begin, mark_end); + content.unindent_and_add(boost::string_ref(mark_begin, mark_end - mark_begin)); if (snippet.id == "!") { @@ -530,7 +529,7 @@ namespace quickbook file_ptr body = f.release(); storage.push_back(template_symbol(snippet->id, params, - qbk_value(body, body->source.begin(), body->source.end(), + qbk_value(body, body->source().begin(), body->source().end(), template_tags::snippet))); } } diff --git a/src/doc_info_actions.cpp b/src/doc_info_actions.cpp index 42ee961..8ceac6c 100644 --- a/src/doc_info_actions.cpp +++ b/src/doc_info_actions.cpp @@ -29,7 +29,7 @@ namespace quickbook static std::string doc_info_output(value const& p, unsigned version) { if (qbk_version_n < version) { - std::string value = p.get_quickbook(); + std::string value = detail::to_s(p.get_quickbook()); value.erase(value.find_last_not_of(" \t") + 1); return value; } @@ -141,7 +141,7 @@ namespace quickbook if (values.check(doc_info_tags::type)) { - doc_type = values.consume(doc_info_tags::type).get_quickbook(); + doc_type = detail::to_s(values.consume(doc_info_tags::type).get_quickbook()); doc_title = values.consume(doc_info_tags::title); use_doc_info = !nested_file || qbk_version_n >= 106u; } @@ -200,9 +200,9 @@ namespace quickbook std::string include_doc_id_, id_; if (!include_doc_id.empty()) - include_doc_id_ = include_doc_id.get_quickbook(); + include_doc_id_ = detail::to_s(include_doc_id.get_quickbook()); if (!id.empty()) - id_ = id.get_quickbook(); + id_ = detail::to_s(id.get_quickbook()); // Quickbook version diff --git a/src/files.cpp b/src/files.cpp index f85b611..e036ced 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -136,11 +136,11 @@ namespace quickbook } file_position relative_position( - std::string::const_iterator begin, - std::string::const_iterator iterator) + boost::string_ref::const_iterator begin, + boost::string_ref::const_iterator iterator) { file_position pos; - std::string::const_iterator line_begin = begin; + boost::string_ref::const_iterator line_begin = begin; while (begin != iterator) { @@ -172,9 +172,9 @@ namespace quickbook return pos; } - file_position file::position_of(std::string::const_iterator iterator) const + file_position file::position_of(boost::string_ref::const_iterator iterator) const { - return relative_position(source.begin(), iterator); + return relative_position(source().begin(), iterator); } // Mapped files. @@ -293,9 +293,9 @@ namespace quickbook file_ptr original; std::vector mapped_sections; - void add_empty_mapped_file_section(std::string::const_iterator pos) { + void add_empty_mapped_file_section(boost::string_ref::const_iterator pos) { std::string::size_type original_pos = - pos - original->source.begin(); + pos - original->source().begin(); if (mapped_sections.empty() || mapped_sections.back().section_type != @@ -303,23 +303,23 @@ namespace quickbook mapped_sections.back().original_pos != original_pos) { mapped_sections.push_back(mapped_file_section( - original_pos, source.size(), + original_pos, source().size(), mapped_file_section::empty)); } } - void add_mapped_file_section(std::string::const_iterator pos) { + void add_mapped_file_section(boost::string_ref::const_iterator pos) { mapped_sections.push_back(mapped_file_section( - pos - original->source.begin(), source.size())); + pos - original->source().begin(), source().size())); } - void add_indented_mapped_file_section(std::string::const_iterator pos) { + void add_indented_mapped_file_section(boost::string_ref::const_iterator pos) { mapped_sections.push_back(mapped_file_section( - pos - original->source.begin(), source.size(), + pos - original->source().begin(), source().size(), mapped_file_section::indented)); } - virtual file_position position_of(std::string::const_iterator) const; + virtual file_position position_of(boost::string_ref::const_iterator) const; }; namespace { @@ -361,43 +361,43 @@ namespace quickbook bool mapped_file_builder::empty() const { - return data->new_file->source.empty(); + return data->new_file->source().empty(); } mapped_file_builder::pos mapped_file_builder::get_pos() const { - return data->new_file->source.size(); + return data->new_file->source().size(); } void mapped_file_builder::add(char const* x, iterator pos) { data->new_file->add_empty_mapped_file_section(pos); - data->new_file->source.append(x); + data->new_file->source_.append(x); } - void mapped_file_builder::add(std::string const& x, iterator pos) + void mapped_file_builder::add(boost::string_ref x, iterator pos) { data->new_file->add_empty_mapped_file_section(pos); - data->new_file->source.append(x); + data->new_file->source_.append(x.begin(), x.end()); } - void mapped_file_builder::add(iterator begin, iterator end) + void mapped_file_builder::add(boost::string_ref x) { - data->new_file->add_mapped_file_section(begin); - data->new_file->source.append(begin, end); + data->new_file->add_mapped_file_section(x.begin()); + data->new_file->source_.append(x.begin(), x.end()); } void mapped_file_builder::add(mapped_file_builder const& x) { - add(x, 0, x.data->new_file->source.size()); + add(x, 0, x.data->new_file->source_.size()); } void mapped_file_builder::add(mapped_file_builder const& x, pos begin, pos end) { assert(data->new_file->original == x.data->new_file->original); - assert(begin <= x.data->new_file->source.size()); - assert(end <= x.data->new_file->source.size()); + assert(begin <= x.data->new_file->source_.size()); + assert(end <= x.data->new_file->source_.size()); if (begin != end) { @@ -407,7 +407,7 @@ namespace quickbook assert(start != x.data->new_file->mapped_sections.begin()); --start; - std::string::size_type size = data->new_file->source.size(); + std::string::size_type size = data->new_file->source_.size(); data->new_file->mapped_sections.push_back(mapped_file_section( start->to_original_pos(begin), size, @@ -421,15 +421,15 @@ namespace quickbook start->section_type)); } - data->new_file->source.append( - x.data->new_file->source.begin() + begin, - x.data->new_file->source.begin() + end); + data->new_file->source_.append( + x.data->new_file->source_.begin() + begin, + x.data->new_file->source_.begin() + end); } } - void mapped_file_builder::unindent_and_add(iterator begin, iterator end) + void mapped_file_builder::unindent_and_add(boost::string_ref x) { - std::string program(begin, end); + std::string program(x.begin(), x.end()); // Erase leading blank lines and newlines: std::string::size_type start = program.find_first_not_of(" \t"); @@ -487,23 +487,23 @@ namespace quickbook program.erase(pos, (std::min)(indent, next-pos)); } - data->new_file->add_indented_mapped_file_section(begin + indent); - data->new_file->source.append(program); + data->new_file->add_indented_mapped_file_section(x.begin() + indent); + data->new_file->source_.append(program); } - file_position mapped_file::position_of(std::string::const_iterator pos) const + file_position mapped_file::position_of(boost::string_ref::const_iterator pos) const { std::vector::const_iterator section = boost::upper_bound(mapped_sections, - std::string::size_type(pos - source.begin()), + std::string::size_type(pos - source().begin()), mapped_section_pos_cmp()); assert(section != mapped_sections.begin()); --section; return section->calculate_position( original->position_of( - original->source.begin() + section->original_pos), - relative_position(source.begin() + section->our_pos, pos) + original->source().begin() + section->original_pos), + relative_position(source().begin() + section->our_pos, pos) ); } } diff --git a/src/files.hpp b/src/files.hpp index 4f217e7..585be30 100644 --- a/src/files.hpp +++ b/src/files.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -41,21 +42,23 @@ namespace quickbook { file(file const&); public: fs::path const path; - std::string source; + std::string source_; bool is_code_snippets; private: unsigned qbk_version; unsigned ref_count; public: + boost::string_ref source() const { return source_; } - file(fs::path const& path, std::string const& source, + file(fs::path const& path, boost::string_ref source, unsigned qbk_version) : - path(path), source(source), is_code_snippets(false), + path(path), source_(source.begin(), source.end()), is_code_snippets(false), qbk_version(qbk_version), ref_count(0) {} - file(file const& f, std::string const& source) : - path(f.path), source(source), is_code_snippets(f.is_code_snippets), + file(file const& f, boost::string_ref source) : + path(f.path), source_(source.begin(), source.end()), + is_code_snippets(f.is_code_snippets), qbk_version(f.qbk_version), ref_count(0) {} @@ -76,7 +79,7 @@ namespace quickbook { qbk_version = v; } - virtual file_position position_of(std::string::const_iterator) const; + virtual file_position position_of(boost::string_ref::const_iterator) const; friend void intrusive_ptr_add_ref(file* ptr) { ++ptr->ref_count; } @@ -101,8 +104,8 @@ namespace quickbook { struct mapped_file_builder { - typedef std::string::const_iterator iterator; - typedef std::string::size_type pos; + typedef boost::string_ref::const_iterator iterator; + typedef boost::string_ref::size_type pos; mapped_file_builder(); ~mapped_file_builder(); @@ -115,11 +118,11 @@ namespace quickbook { pos get_pos() const; void add(char const*, iterator); - void add(std::string const&, iterator); - void add(iterator, iterator); + void add(boost::string_ref, iterator); + void add(boost::string_ref); void add(mapped_file_builder const&); void add(mapped_file_builder const&, pos, pos); - void unindent_and_add(iterator, iterator); + void unindent_and_add(boost::string_ref); private: mapped_file_builder_data* data; diff --git a/src/fwd.hpp b/src/fwd.hpp index 7cccde8..2fce3ee 100644 --- a/src/fwd.hpp +++ b/src/fwd.hpp @@ -13,6 +13,7 @@ #include "iterator.hpp" #include +#include namespace quickbook { @@ -25,7 +26,7 @@ namespace quickbook struct template_symbol; typedef boost::intrusive_ptr file_ptr; - typedef std::string::const_iterator string_iterator; + typedef boost::string_ref::const_iterator string_iterator; typedef lookback_iterator parse_iterator; inline void ignore_variable(void const*) {} diff --git a/src/id_manager.cpp b/src/id_manager.cpp index 3b2f601..1f84ad1 100644 --- a/src/id_manager.cpp +++ b/src/id_manager.cpp @@ -8,7 +8,7 @@ #include "id_manager.hpp" #include "utils.hpp" -#include "string_ref.hpp" +#include #include #include #include @@ -29,9 +29,9 @@ namespace quickbook struct id_placeholder; struct id_data; - std::string replace_ids(id_state& state, std::string const& xml, + std::string replace_ids(id_state& state, boost::string_ref xml, bool use_resolved_ids = true); - std::string process_ids(id_state&, std::string const&); + std::string process_ids(id_state&, boost::string_ref); static const std::size_t max_size = 32; @@ -68,13 +68,15 @@ namespace quickbook id_placeholder( unsigned index, - std::string const& id, + boost::string_ref id, id_category category, id_placeholder* parent_ = 0) : index(index), generation_state(parent_ ? child : unresolved), - unresolved_id(parent_ ? parent_->unresolved_id + '.' + id : id), - id(id), + unresolved_id(parent_ ? + parent_->unresolved_id + '.' + detail::to_s(id) : + detail::to_s(id)), + id(id.begin(), id.end()), parent(parent_), category(category), num_dots(boost::range::count(id, '.') + @@ -118,39 +120,39 @@ namespace quickbook // Placeholder methods id_placeholder* add_placeholder( - std::string const&, id_category, id_placeholder* parent = 0); + boost::string_ref, id_category, id_placeholder* parent = 0); - id_placeholder* get_placeholder(string_ref); + id_placeholder* get_placeholder(boost::string_ref); // Events id_placeholder* start_file( unsigned compatibility_version, bool document_root, - std::string const& include_doc_id, - std::string const& id, + boost::string_ref include_doc_id, + boost::string_ref id, value const& title); void end_file(); id_placeholder* add_id( - std::string const& id, + boost::string_ref id, id_category category); id_placeholder* old_style_id( - std::string const& id, + boost::string_ref id, id_category category); id_placeholder* begin_section( - std::string const& id, + boost::string_ref id, id_category category); void end_section(); private: id_placeholder* add_id_to_section( - std::string const& id, + boost::string_ref id, id_category category, boost::shared_ptr const& section); id_placeholder* create_new_section( - std::string const& id, + boost::string_ref id, id_category category); void switch_section(id_placeholder*); @@ -210,7 +212,7 @@ private: id_placeholder* placeholder_1_6; section_info(boost::shared_ptr const& parent, - unsigned compatibility_version, std::string const& id) : + unsigned compatibility_version, boost::string_ref id) : parent(parent), compatibility_version(compatibility_version), level(parent ? parent->level + 1 : 1), id_1_1(), placeholder_1_6(0) @@ -219,7 +221,7 @@ private: id_1_1 = parent->id_1_1; if (!id_1_1.empty() && !id.empty()) id_1_1 += "."; - id_1_1 += id; + id_1_1.append(id.begin(), id.end()); } } }; @@ -237,8 +239,8 @@ private: void id_manager::start_file( unsigned compatibility_version, - std::string const& include_doc_id, - std::string const& id, + boost::string_ref include_doc_id, + boost::string_ref id, value const& title) { state->start_file(compatibility_version, false, include_doc_id, id, title); @@ -246,8 +248,8 @@ private: std::string id_manager::start_file_with_docinfo( unsigned compatibility_version, - std::string const& include_doc_id, - std::string const& id, + boost::string_ref include_doc_id, + boost::string_ref id, value const& title) { return state->start_file(compatibility_version, true, include_doc_id, @@ -259,7 +261,7 @@ private: state->end_file(); } - std::string id_manager::begin_section(std::string const& id, + std::string id_manager::begin_section(boost::string_ref id, id_category category) { return state->begin_section(id, category)->to_string(); @@ -275,28 +277,28 @@ private: return state->current_file->document->current_section->level; } - std::string id_manager::old_style_id(std::string const& id, id_category category) + std::string id_manager::old_style_id(boost::string_ref id, id_category category) { return state->old_style_id(id, category)->to_string(); } - std::string id_manager::add_id(std::string const& id, id_category category) + std::string id_manager::add_id(boost::string_ref id, id_category category) { return state->add_id(id, category)->to_string(); } - std::string id_manager::add_anchor(std::string const& id, id_category category) + std::string id_manager::add_anchor(boost::string_ref id, id_category category) { return state->add_placeholder(id, category)->to_string(); } std::string id_manager::replace_placeholders_with_unresolved_ids( - std::string const& xml) const + boost::string_ref xml) const { return replace_ids(*state, xml, false); } - std::string id_manager::replace_placeholders(std::string const& xml) const + std::string id_manager::replace_placeholders(boost::string_ref xml) const { assert(!state->current_file); return process_ids(*state, xml); @@ -316,12 +318,11 @@ private: namespace { std::string normalize_id( - std::string src_id, + boost::string_ref src_id, std::size_t prefix = 0, std::size_t size = max_size) { - std::string id; - id.swap(src_id); + std::string id(src_id.begin(), src_id.end()); std::size_t src = prefix; std::size_t dst = prefix; @@ -364,7 +365,7 @@ private: // id_placeholder* id_state::add_placeholder( - std::string const& id, id_category category, + boost::string_ref id, id_category category, id_placeholder* parent) { placeholders.push_back(id_placeholder( @@ -372,7 +373,7 @@ private: return &placeholders.back(); } - id_placeholder* id_state::get_placeholder(string_ref value) + id_placeholder* id_state::get_placeholder(boost::string_ref value) { // If this isn't a placeholder id. if (value.size() <= 1 || *value.begin() != '$') @@ -429,8 +430,8 @@ private: id_placeholder* id_state::start_file( unsigned compatibility_version, bool document_root, - std::string const& include_doc_id, - std::string const& id, + boost::string_ref include_doc_id, + boost::string_ref id, value const& title) { // Create new file @@ -451,7 +452,7 @@ private: // specified in an 'include' element) unless backwards compatibility // is required. - std::string initial_doc_id; + boost::string_ref initial_doc_id; if (document_root || compatibility_version >= 106u || @@ -471,9 +472,9 @@ private: if (title.check()) current_file->document->last_title_1_1 = - title.get_quickbook(); + detail::to_s(title.get_quickbook()); - current_file->doc_id_1_1 = !initial_doc_id.empty() ? initial_doc_id : + current_file->doc_id_1_1 = !initial_doc_id.empty() ? detail::to_s(initial_doc_id) : detail::make_identifier(current_file->document->last_title_1_1); } else if (parent) { @@ -519,7 +520,7 @@ private: } id_placeholder* id_state::add_id( - std::string const& id, + boost::string_ref id, id_category category) { return add_id_to_section(id, category, @@ -527,11 +528,11 @@ private: } id_placeholder* id_state::add_id_to_section( - std::string const& id, + boost::string_ref id, id_category category, boost::shared_ptr const& section) { - std::string id_part = id; + std::string id_part(id.begin(), id.end()); // Note: Normalizing id according to file compatibility version, but // adding to section according to section compatibility version. @@ -562,25 +563,25 @@ private: } id_placeholder* id_state::old_style_id( - std::string const& id, + boost::string_ref id, id_category category) { return current_file->compatibility_version < 103u ? add_placeholder( - current_file->document->section_id_1_1 + "." + id, category) : + current_file->document->section_id_1_1 + "." + detail::to_s(id), category) : add_id(id, category); } id_placeholder* id_state::begin_section( - std::string const& id, + boost::string_ref id, id_category category) { - current_file->document->section_id_1_1 = id; + current_file->document->section_id_1_1 = detail::to_s(id); return create_new_section(id, category); } id_placeholder* id_state::create_new_section( - std::string const& id, + boost::string_ref id, id_category category) { boost::shared_ptr parent = @@ -618,7 +619,7 @@ private: if (parent && !new_section->placeholder_1_6) new_id = current_file->doc_id_1_1 + '.'; - new_id += id; + new_id += detail::to_s(id); p = add_placeholder(new_id, category, new_section->placeholder_1_6); @@ -654,13 +655,13 @@ private: std::vector id_attributes; struct callback { - virtual void start(string_ref) {} - virtual void id_value(string_ref) {} - virtual void finish(string_ref) {} + virtual void start(boost::string_ref) {} + virtual void id_value(boost::string_ref) {} + virtual void finish(boost::string_ref) {} virtual ~callback() {} }; - void parse(std::string const&, callback&); + void parse(boost::string_ref, callback&); }; namespace @@ -724,14 +725,13 @@ private: while(it != end && !find_char(text, *it)) ++it; } - void xml_processor::parse(std::string const& source, callback& c) + void xml_processor::parse(boost::string_ref source, callback& c) { - typedef std::string::const_iterator iterator; + typedef boost::string_ref::const_iterator iterator; - string_ref source_ref(source.begin(), source.end()); - c.start(source_ref); + c.start(source); - iterator it = source_ref.begin(), end = source_ref.end(); + iterator it = source.begin(), end = source.end(); for(;;) { @@ -770,7 +770,7 @@ private: iterator name_start = it; read_to_one_of(it, end, "= \t\n\r>"); if (it == end || *it == '>') break; - string_ref name(name_start, it); + boost::string_ref name(name_start, it - name_start); ++it; read_some_of(it, end, "= \t\n\r"); @@ -783,10 +783,10 @@ private: it = std::find(it, end, delim); if (it == end) break; - string_ref value(value_start, it); + boost::string_ref value(value_start, it - value_start); ++it; - if (boost::find(id_attributes, name) + if (boost::find(id_attributes, detail::to_s(name)) != id_attributes.end()) { c.id_value(value); @@ -800,7 +800,7 @@ private: } } - c.finish(source_ref); + c.finish(source); } // @@ -813,7 +813,7 @@ private: struct id_generation_data { - id_generation_data(std::string const& src_id) + id_generation_data(boost::string_ref src_id) : child_start(src_id.rfind('.') + 1), id(normalize_id(src_id, child_start, max_size - 1)), // 'max_size - 1' leaves a character to append @@ -878,11 +878,11 @@ private: typedef boost::unordered_map allocated_ids; typedef std::vector placeholder_index; - placeholder_index index_placeholders(id_state&, std::string const& xml); + placeholder_index index_placeholders(id_state&, boost::string_ref xml); void resolve_id(id_placeholder&, allocated_ids&); void generate_id(id_placeholder&, allocated_ids&); - std::string process_ids(id_state& state, std::string const& xml) + std::string process_ids(id_state& state, boost::string_ref xml) { placeholder_index placeholders = index_placeholders(state, xml); @@ -947,7 +947,7 @@ private: count(0) {} - void id_value(string_ref value) + void id_value(boost::string_ref value) { id_placeholder* p = state.get_placeholder(value); number(p); @@ -964,7 +964,7 @@ private: placeholder_index index_placeholders( id_state& state, - std::string const& xml) + boost::string_ref xml) { xml_processor processor; number_placeholders_callback callback(state); @@ -1095,7 +1095,7 @@ private: { id_state& state; bool use_resolved_ids; - std::string::const_iterator source_pos; + boost::string_ref::const_iterator source_pos; std::string result; replace_ids_callback(id_state& state, bool resolved) @@ -1105,18 +1105,18 @@ private: result() {} - void start(string_ref xml) + void start(boost::string_ref xml) { source_pos = xml.begin(); } - void id_value(string_ref value) + void id_value(boost::string_ref value) { if (id_placeholder* p = state.get_placeholder(value)) { assert(!use_resolved_ids || p->check_state(id_placeholder::generated)); - std::string const& id = use_resolved_ids ? + boost::string_ref id = use_resolved_ids ? p->id : p->unresolved_id; result.append(source_pos, value.begin()); @@ -1125,14 +1125,14 @@ private: } } - void finish(string_ref xml) + void finish(boost::string_ref xml) { result.append(source_pos, xml.end()); source_pos = xml.end(); } }; - std::string replace_ids(id_state& state, std::string const& xml, + std::string replace_ids(id_state& state, boost::string_ref xml, bool use_unresolved_ids) { xml_processor processor; diff --git a/src/id_manager.hpp b/src/id_manager.hpp index e071241..9410d23 100644 --- a/src/id_manager.hpp +++ b/src/id_manager.hpp @@ -10,6 +10,7 @@ #define BOOST_QUICKBOOK_ID_MANAGER_HPP #include +#include #include #include "values.hpp" @@ -53,29 +54,29 @@ namespace quickbook std::string start_file_with_docinfo( unsigned compatibility_version, - std::string const& include_doc_id, - std::string const& id, + boost::string_ref include_doc_id, + boost::string_ref id, value const& title); void start_file( unsigned compatibility_version, - std::string const& include_doc_id, - std::string const& id, + boost::string_ref include_doc_id, + boost::string_ref id, value const& title); void end_file(); - std::string begin_section(std::string const&, id_category); + std::string begin_section(boost::string_ref, id_category); void end_section(); int section_level() const; - std::string old_style_id(std::string const&, id_category); - std::string add_id(std::string const&, id_category); - std::string add_anchor(std::string const&, id_category); + std::string old_style_id(boost::string_ref, id_category); + std::string add_id(boost::string_ref, id_category); + std::string add_anchor(boost::string_ref, id_category); std::string replace_placeholders_with_unresolved_ids( - std::string const&) const; - std::string replace_placeholders(std::string const&) const; + boost::string_ref) const; + std::string replace_placeholders(boost::string_ref) const; unsigned compatibility_version() const; private: diff --git a/src/input_path.cpp b/src/input_path.cpp index 9b6a877..3fa8717 100644 --- a/src/input_path.cpp +++ b/src/input_path.cpp @@ -51,8 +51,9 @@ namespace detail { return std::string(buffer.get()); } - std::wstring from_utf8(std::string const& x) + std::wstring from_utf8(boost::string_ref text) { + std::string x(text.begin(), text.end()); int buffer_count = MultiByteToWideChar(CP_UTF8, 0, x.c_str(), -1, 0, 0); if (!buffer_count) @@ -81,7 +82,7 @@ namespace detail { #endif #if QUICKBOOK_WIDE_PATHS - fs::path generic_to_path(std::string const& x) + fs::path generic_to_path(boost::string_ref x) { return fs::path(from_utf8(x)); } @@ -91,9 +92,9 @@ namespace detail { return to_utf8(x.generic_wstring()); } #else - fs::path generic_to_path(std::string const& x) + fs::path generic_to_path(boost::string_ref x) { - return fs::path(x); + return fs::path(x.begin(), x.end()); } std::string path_to_generic(fs::path const& x) @@ -166,7 +167,7 @@ namespace detail { if (_isatty(_fileno(stderr))) _setmode(_fileno(stderr), _O_U16TEXT); } - void write_utf8(ostream::base_ostream& out, std::string const& x) + void write_utf8(ostream::base_ostream& out, boost::string_ref x) { out << from_utf8(x); } @@ -192,7 +193,7 @@ namespace detail { { } - void write_utf8(ostream::base_ostream& out, std::string const& x) + void write_utf8(ostream::base_ostream& out, boost::string_ref x) { out << x; } @@ -281,6 +282,11 @@ namespace detail { return *this; } + ostream& ostream::operator<<(boost::string_ref x) { + write_utf8(base, x); + return *this; + } + ostream& ostream::operator<<(int x) { base << x; return *this; diff --git a/src/input_path.hpp b/src/input_path.hpp index a9b55f6..6529514 100644 --- a/src/input_path.hpp +++ b/src/input_path.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -62,8 +63,10 @@ namespace quickbook #if QUICKBOOK_WIDE_PATHS typedef std::wstring input_string; + typedef boost::wstring_ref input_string_ref; #else typedef std::string input_string; + typedef boost::string_ref input_string_ref; #endif // A light wrapper around C++'s streams that gets things right @@ -76,10 +79,12 @@ namespace quickbook typedef std::wostream base_ostream; typedef std::wios base_ios; typedef std::wstring string; + typedef boost::wstring_ref string_ref; #else typedef std::ostream base_ostream; typedef std::ios base_ios; typedef std::string string; + typedef boost::string_ref string_ref; #endif base_ostream& base; @@ -91,6 +96,7 @@ namespace quickbook // std::string should be UTF-8 (what a mess!) ostream& operator<<(std::string const&); + ostream& operator<<(boost::string_ref); // Other value types. ostream& operator<<(int x); @@ -109,7 +115,7 @@ namespace quickbook fs::path input_to_path(input_string const&); std::string path_to_generic(fs::path const&); - fs::path generic_to_path(std::string const&); + fs::path generic_to_path(boost::string_ref); void initialise_output(); diff --git a/src/main_grammar.cpp b/src/main_grammar.cpp index 74dbece..c494350 100644 --- a/src/main_grammar.cpp +++ b/src/main_grammar.cpp @@ -224,7 +224,7 @@ namespace quickbook info_.type != element_info::maybe_block) { l.state_.source_mode.swap(saved_source_mode_); - l.state_.source_mode = l.state_.source_mode_next.get_quickbook(); + l.state_.source_mode = detail::to_s(l.state_.source_mode_next.get_quickbook()); l.state_.source_mode_next = value(); } diff --git a/src/quickbook.cpp b/src/quickbook.cpp index 81b32e4..855fbbd 100644 --- a/src/quickbook.cpp +++ b/src/quickbook.cpp @@ -61,8 +61,9 @@ namespace quickbook end = preset_defines.end(); it != end; ++it) { - parse_iterator first(it->begin()); - parse_iterator last(it->end()); + boost::string_ref val(*it); + parse_iterator first(val.begin()); + parse_iterator last(val.end()); cl::parse_info info = cl::parse(first, last, state.grammar().command_line_macro); @@ -85,8 +86,8 @@ namespace quickbook /////////////////////////////////////////////////////////////////////////// void parse_file(quickbook::state& state, value include_doc_id, bool nested_file) { - parse_iterator first(state.current_file->source.begin()); - parse_iterator last(state.current_file->source.end()); + parse_iterator first(state.current_file->source().begin()); + parse_iterator last(state.current_file->source().end()); cl::parse_info info = cl::parse(first, last, state.grammar().doc_info); assert(info.hit); diff --git a/src/string_ref.cpp b/src/string_ref.cpp deleted file mode 100644 index 6c33df1..0000000 --- a/src/string_ref.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/*============================================================================= - Copyright (c) 2011 Daniel James - - Use, modification and distribution is subject to the Boost Software - License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) -=============================================================================*/ - -#include "string_ref.hpp" -#include -#include -#include -#include - -namespace quickbook -{ - void string_ref::swap(string_ref& x) - { - boost::swap(begin_, x.begin_); - boost::swap(end_, x.end_); - } - - bool operator==(string_ref const& x, string_ref const& y) - { - return boost::equal(x, y); - } - - bool operator<(string_ref const& x, string_ref const& y) - { - return boost::lexicographical_compare(x, y); - } - - std::ostream& operator<<(std::ostream& out, string_ref const& x) - { - return out.write(&*x.begin(), x.end() - x.begin()); - } -} diff --git a/src/string_ref.hpp b/src/string_ref.hpp deleted file mode 100644 index ffb95bf..0000000 --- a/src/string_ref.hpp +++ /dev/null @@ -1,89 +0,0 @@ -/*============================================================================= - Copyright (c) 2011 Daniel James - - Use, modification and distribution is subject to the Boost Software - License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) -=============================================================================*/ - -#if !defined(BOOST_QUICKBOOK_STRING_REF_HPP) -#define BOOST_QUICKBOOK_STRING_REF_HPP - -#include -#include -#include - -namespace quickbook -{ - struct string_ref - : boost::less_than_comparable > > > - { - public: - typedef std::string::const_iterator iterator; - typedef std::string::const_iterator const_iterator; - - private: - iterator begin_, end_; - - public: - string_ref() : begin_(), end_() {} - - explicit string_ref(iterator b, iterator e) - : begin_(b), end_(e) {} - - explicit string_ref(std::string const& x) - : begin_(x.begin()), end_(x.end()) {} - - void swap(string_ref&); - - void clear() { - begin_ = end_ = iterator(); - } - - operator std::string() const { - return std::string(begin_, end_); - } - - iterator begin() const { return begin_; } - iterator end() const { return end_; } - - std::size_t size() const - { - return static_cast(end_ - begin_); - } - - bool empty() const - { - return begin_ == end_; - } - }; - - bool operator==(string_ref const& x, string_ref const& y); - bool operator<(string_ref const& x, string_ref const& y); - std::ostream& operator<<(std::ostream&, string_ref const& x); - - inline bool operator==(string_ref const& x, std::string const& y) - { - return x == string_ref(y); - } - - inline bool operator<(string_ref const& x, std::string const& y) - { - return x < string_ref(y); - } - - inline bool operator>(string_ref const& x, std::string const& y) - { - return x > string_ref(y); - } - - inline void swap(string_ref& x, string_ref& y) - { - x.swap(y); - } -} - -#endif diff --git a/src/syntax_highlight.cpp b/src/syntax_highlight.cpp index e618c07..bb164c5 100644 --- a/src/syntax_highlight.cpp +++ b/src/syntax_highlight.cpp @@ -93,7 +93,7 @@ namespace quickbook // State bool support_callouts; - string_ref marked_text; + boost::string_ref marked_text; syntax_highlight_actions(quickbook::state& state, bool is_block) : out(), state(state), @@ -186,7 +186,7 @@ namespace quickbook void syntax_highlight_actions::mark_text(parse_iterator first, parse_iterator last) { - marked_text = string_ref(first.base(), last.base()); + marked_text = boost::string_ref(first.base(), last.base() - first.base()); } void syntax_highlight_actions::callout(parse_iterator, parse_iterator) diff --git a/src/utils.cpp b/src/utils.cpp index 3a5ee42..9bd0bee 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -29,9 +29,9 @@ namespace quickbook { namespace detail } } - void print_string(std::basic_string const& str, std::ostream& out) + void print_string(boost::string_ref const& str, std::ostream& out) { - for (std::string::const_iterator cur = str.begin(); + for (boost::string_ref::const_iterator cur = str.begin(); cur != str.end(); ++cur) { print_char(*cur, out); diff --git a/src/utils.hpp b/src/utils.hpp index 9170f81..d867747 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -14,10 +14,11 @@ #include #include #include +#include namespace quickbook { namespace detail { void print_char(char ch, std::ostream& out); - void print_string(std::basic_string const& str, std::ostream& out); + void print_string(boost::string_ref const& str, std::ostream& out); char filter_identifier_char(char ch); template @@ -33,6 +34,13 @@ namespace quickbook { namespace detail { } std::string escape_uri(std::string uri); + inline std::string escape_uri(boost::string_ref const& uri) { + return escape_uri(std::string(uri.begin(), uri.end())); + } + + inline std::string to_s(boost::string_ref x) { + return std::string(x.begin(), x.end()); + } }} #endif // BOOST_SPIRIT_QUICKBOOK_UTILS_HPP diff --git a/src/values.cpp b/src/values.cpp index 904cc8b..59c11cf 100644 --- a/src/values.cpp +++ b/src/values.cpp @@ -50,7 +50,7 @@ namespace quickbook file_ptr value_node::get_file() const { UNDEFINED_ERROR(); } string_iterator value_node::get_position() const { UNDEFINED_ERROR(); } int value_node::get_int() const { UNDEFINED_ERROR(); } - string_ref value_node::get_quickbook() const { UNDEFINED_ERROR(); } + boost::string_ref value_node::get_quickbook() const { UNDEFINED_ERROR(); } std::string value_node::get_encoded() const { UNDEFINED_ERROR(); } value_node* value_node::get_list() const { UNDEFINED_ERROR(); } @@ -332,7 +332,7 @@ namespace quickbook virtual value_node* clone() const; virtual file_ptr get_file() const; virtual string_iterator get_position() const; - virtual string_ref get_quickbook() const; + virtual boost::string_ref get_quickbook() const; virtual bool empty() const; virtual bool equals(value_node*) const; @@ -354,7 +354,7 @@ namespace quickbook virtual value_node* clone() const; virtual file_ptr get_file() const; virtual string_iterator get_position() const; - virtual string_ref get_quickbook() const; + virtual boost::string_ref get_quickbook() const; virtual std::string get_encoded() const; virtual bool empty() const; virtual bool is_encoded() const; @@ -433,8 +433,8 @@ namespace quickbook string_iterator qbk_value_impl::get_position() const { return begin_; } - string_ref qbk_value_impl::get_quickbook() const - { return string_ref(begin_, end_); } + boost::string_ref qbk_value_impl::get_quickbook() const + { return boost::string_ref(begin_, end_ - begin_); } bool qbk_value_impl::empty() const { return begin_ == end_; } @@ -481,8 +481,8 @@ namespace quickbook string_iterator encoded_qbk_value_impl::get_position() const { return begin_; } - string_ref encoded_qbk_value_impl::get_quickbook() const - { return string_ref(begin_, end_); } + boost::string_ref encoded_qbk_value_impl::get_quickbook() const + { return boost::string_ref(begin_, end_ - begin_); } std::string encoded_qbk_value_impl::get_encoded() const { return encoded_value_; } diff --git a/src/values.hpp b/src/values.hpp index d637ea2..e681c8a 100644 --- a/src/values.hpp +++ b/src/values.hpp @@ -16,9 +16,9 @@ #include #include #include +#include #include #include "fwd.hpp" -#include "string_ref.hpp" #include "files.hpp" namespace quickbook @@ -51,7 +51,7 @@ namespace quickbook virtual file_ptr get_file() const; virtual string_iterator get_position() const; - virtual string_ref get_quickbook() const; + virtual boost::string_ref get_quickbook() const; virtual std::string get_encoded() const; virtual int get_int() const; @@ -113,7 +113,7 @@ namespace quickbook { return value_->get_file(); } string_iterator get_position() const { return value_->get_position(); } - string_ref get_quickbook() const + boost::string_ref get_quickbook() const { return value_->get_quickbook(); } std::string get_encoded() const { return value_->get_encoded(); } diff --git a/test/unit/Jamfile.v2 b/test/unit/Jamfile.v2 index 0b583b7..0ed1694 100644 --- a/test/unit/Jamfile.v2 +++ b/test/unit/Jamfile.v2 @@ -20,7 +20,7 @@ project quickbook-unit-tests darwin:BOOST_DETAIL_CONTAINER_FWD ; -run values_test.cpp ../../src/values.cpp ../../src/files.cpp ../../src/string_ref.cpp ; +run values_test.cpp ../../src/values.cpp ../../src/files.cpp ; run post_process_test.cpp ../../src/post_process.cpp ; # Copied from spirit diff --git a/test/unit/values_test.cpp b/test/unit/values_test.cpp index b6d186c..d3da4a8 100644 --- a/test/unit/values_test.cpp +++ b/test/unit/values_test.cpp @@ -32,10 +32,10 @@ void qbk_tests() "(fake file)", source, 105u); q = quickbook::qbk_value( fake_file, - fake_file->source.begin(), - fake_file->source.end()); + fake_file->source().begin(), + fake_file->source().end()); } - BOOST_TEST_EQ(q.get_quickbook(), source); + BOOST_TEST_EQ(q.get_quickbook(), boost::string_ref(source)); } void sort_test()