From 9f7baaec47db739cabd54effec3a9e5f0a068627 Mon Sep 17 00:00:00 2001 From: "olafvdspek@gmail.com" Date: Sat, 25 Feb 2012 16:28:13 +0000 Subject: [PATCH] Add begin(), end(), data() and size() to TemplateString --- src/ctemplate/template_string.h.in | 17 ++++++++++++++++- src/template.cc | 8 ++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ctemplate/template_string.h.in b/src/ctemplate/template_string.h.in index 836f7be..0137810 100644 --- a/src/ctemplate/template_string.h.in +++ b/src/ctemplate/template_string.h.in @@ -187,6 +187,22 @@ class @ac_windows_dllexport@ TemplateString { is_immutable_(true), id_(s.do_not_use_directly_.id_) { } + const char* begin() const { + return ptr_; + } + + const char* end() const { + return ptr_ + length_; + } + + const char* data() const { + return ptr_; + } + + size_t size() const { + return length_; + } + inline bool empty() const { return length_ == 0; }; @@ -198,7 +214,6 @@ class @ac_windows_dllexport@ TemplateString { // Only TemplateDictionaries and template expansion code can read these. friend class TemplateDictionary; friend class TemplateDictionaryPeer; // TDP::GetSectionValue() - friend class Template; // for StringToTemplate() friend class VariableTemplateNode; // VTN::Expand() friend class TemplateCache; // for GetGlobalId friend class StaticTemplateStringInitializer; // for AddToGlo... diff --git a/src/template.cc b/src/template.cc index 67ee8a4..f3bda67 100644 --- a/src/template.cc +++ b/src/template.cc @@ -2141,9 +2141,9 @@ Template* Template::StringToTemplate(const TemplateString& content, // But we have to do the "loading" and parsing ourselves: // BuildTree deletes the buffer when done, so we need a copy for it. - char* buffer = new char[content.length_]; - size_t content_len = content.length_; - memcpy(buffer, content.ptr_, content_len); + char* buffer = new char[content.size()]; + size_t content_len = content.size(); + memcpy(buffer, content.data(), content_len); tpl->StripBuffer(&buffer, &content_len); if ( tpl->BuildTree(buffer, buffer + content_len) ) { assert(tpl->state() == TS_READY); @@ -2171,7 +2171,7 @@ Template* Template::StringToTemplate(const TemplateString& content, Template::Template(const TemplateString& filename, Strip strip, TemplateCache* owner) // TODO(csilvers): replace ToString() with an is_immutable() check - : original_filename_(filename.ToString()), resolved_filename_(), + : original_filename_(filename.data(), filename.size()), resolved_filename_(), filename_mtime_(0), strip_(strip), state_(TS_EMPTY), template_cache_(owner), template_text_(NULL), template_text_len_(0), tree_(NULL), parse_state_(),