C++17 introduced lightweight, non-owning strings referred to as
string_view to the standard. Unlike
std::string, which performs memory allocations
and copies for most string operations (such as
string_view only observes and does not modify the entry.
This can massively decrease the memory footprint and provide a large optimization for immutable strings for things like parsing and tokenization. As a
result, such a type can be an asset in older c++ versions for systems that may not support the newer standards.
The full type,
basic_string_view is templated on a both
Traits to allow viewing of contiguous char-like sequences of data, and for
simple conversion between
string_view implementation is from the BackportCpp library, which aims to
backport library types and utilities from C++14, C++17, and C++20 to be compatible with C++11 compilers. This standalone
library simply offers a subset for those looking for a simple drop-in
C++17 is still undergoing the standardization process (although currently is considered 'feature-complete'). Although non-owning strings are available in many other libraries, such as boost, Bloomberg STD, QT -- all of them come with rather large dependencies and have slightly different functionality and signatures.
By releasing this library as a standalone include, it provides future-proof support to older compilers with easy accessibility. It also allows using the newer feature-set in older versions, and facilitates easy upgrades to the new standard as compilers upgrade and newer c++ versions become available.
The following compilers are currently being tested through continuous integration with Travis.
bpstd::string_view only works on compiler that provide proper conformance for c++11, meaning this
does not properly work on g++ before 4.8
|g++ 4.9.3||Ubuntu 14.04.3 TLS|
|g++ 5.3.0||Ubuntu 14.04.3 TLS|
|g++ 6.1.1||Ubuntu 14.04.3 TLS|
|clang 3.5.0||Ubuntu 14.04.3 TLS|
|clang 3.6.2||Ubuntu 14.04.3 TLS|
|clang 3.8.0||Ubuntu 14.04.3 TLS|
|clang xcode 6.0||Darwin Kernel 13.4.0 (OSX 10.9.5)|
|clang xcode 6.1||Darwin Kernel 14.3.0 (OSX 10.10.3)|
|clang xcode 7.0||Darwin Kernel 14.5.0 (OSX 10.10.5)|
|clang xcode 7.3||Darwin Kernel 15.5.0 (OSX 10.11.5)|
|clang xcode 8.0||Darwin Kernel 15.6.0 (OSX 10.11.6)|
|Visual Studio 14 2015||Windows Server 2012 R2 (x64)|
The class is licensed under the MIT License:
Copyright © 2016-2021 Matthew Rodusek
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.