This is readme file for char_view - immutable string view class with compile-time processing with support for C++ string literals, std::strings and char buffers. Library is currently implemented as header-only solution.
Software is published under BSD license, see license.txt for details.
https://github.com/vpiotr/char_view
- for lightweight string literal handling in C++ way (with functions from std::string)
- to support compile-time properties (length, hash) and search methods (find, contains, equals, etc)
- to support strings in switch statement
-
allows compile-time string function calculations:
using namespace sbt; // check if string has prefix "std::" constexpr bool HasStdPrefix(const char_view &value) { return value.starts_with("std::"_cv); } // assert calculated in compile time bool TestPrefix() { constexpr char_view s1("std::list"); Assert(HasStdPrefix(s1), "HasStdPrefix"); return true; }
-
passing parameter as literal does not involve deep copying - has complexity = O(1)
-
allows using strings in switch statement:
bool is_command_ok(const std::string &str) { switch (char_view(str.c_str()).hash_code()) { case char_view("clear").hash_code(): case char_view("print").hash_code(): case char_view("list").hash_code(): break; default: return false; } return true; }
-
implements special string literal for easy construction:
constexpr auto s1 = "Test string"_cv;
- size of any char_view object is 8 for GCC x32 (size_t + one pointer)
- does not use dynamic memory allocation in all cases when std::string is not involved
- range checking & throwing on error is optional - implemented as policies (template parameters)
- works with various char types: char, wchar_t, char16_t, char32_t
- can be used with literals, char arrays, zstrings & std::string
- compiler: any compiler with C++11 support (constexpr required)
- tested with GCC 4.8.1 with C++11 support active (-std=c++11)
- test build project prepared in Code::Blocks 13
- just C++ standard library, including std::basic_string
In cases where code can be calculated in compile time (literals processing) functions using char_view will be much much faster than any other functions. In best cases functions return just single value calculated during compilation.
- not all overloads of std::string are implemented for functions like find_zzz (substr should be enough)
- class does not check if provided "char *" pointer is literal or something else, so any char pointer can be provided and memory control must be done elsewhere
- articles
- Constants and Constant Expressions in C++11 - Mikhail Semenov, 2012 - article describing constexpr
- Parsing strings at compile-time — Part I - Andrzej Krzemieñski, 2011 - article describing how to employ constexpr for string processing
- papers
- N4236, std::basic_string_literal - standard proposition by Michael Price, 2014
- N4121, std::string_literal - Andrew Tomazos, 2014
- c++14 proposal - string_view
- experimental standard part(?), excluded from C++14
- open source solutions
- constexpr_string - compile time string processing library
- char_view<> at sourceforge - thread-safe, immutable string class by Maxim Yegorushkin
- Boost string_literal - literal wrapper by Andrey Semashev
- Boost char_view - class for testing(?) - by Gennadiy Rozental
- Chromium.org StringPiece
- MNMLSTC Core string_view
- Bloomberg bslstl::StringRef
To build documentation, use Code::Blocks 13 and command:
DoxyBlocks \ Extract documentation
Output will be generated into: "./help/html/" directory in html format.
See the CHANGELOG.