From 5c4b6678e48fd82c10b4ef164fe121348c7cfcbb Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 30 Oct 2014 07:05:42 -0700 Subject: [PATCH] Compute StringRef size in ctor --- format.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/format.h b/format.h index 31325524..0c4c63da 100644 --- a/format.h +++ b/format.h @@ -164,15 +164,20 @@ template class BasicStringRef { private: const Char *data_; - mutable std::size_t size_; + std::size_t size_; public: /** Constructs a string reference object from a C string and a size. - If *size* is zero, which is the default, the size is computed - automatically. */ - BasicStringRef(const Char *s, std::size_t size = 0) : data_(s), size_(size) {} + BasicStringRef(const Char *s, std::size_t n) : data_(s), size_(n) {} + + /** + Constructs a string reference object from a C string computing + the size with ``std::char_traits::length``. + */ + BasicStringRef(const Char *s) + : data_(s), size_(std::char_traits::length(s)) {} /** Constructs a string reference from an `std::string` object. @@ -195,10 +200,7 @@ class BasicStringRef { /** Returns the string size. */ - std::size_t size() const { - if (size_ == 0 && data_) size_ = std::char_traits::length(data_); - return size_; - } + std::size_t size() const { return size_; } friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) { return lhs.data_ == rhs.data_;