Note: this library was previously named string-bare, the original string-lite has been renamed to string-non-lite.
Another attempt at a hopefully generally useful C++ string algorithm library.
I'm still pondering to add functions that take a regular expression, as std::regex
and as string (using *_re()
function names), and the API to use for that.
For now, have a look at section Documentation of string lite and section string-lite test specification for the functions envisioned / implemented (at the moment). The in-place modification class of functions is decidedly absent.
In general, functions take string_view
s and thereby char const *
, std::string
and std::string_view
(note 1) as arguments and produce (return) a bool
, size_t
, std::string
or a collection of string_view
s.
Note 1: to support use of string_view
s with C++ versions earlier than C++17, string_view
s may be accessed as nonstd::string::std17::string_view
, supplying a local string_view
class or std::string_view
if present.
Contents
- Example usage
- In a nutshell
- License
- Dependencies
- Installation and use
- Synopsis
- Notes and references
- Appendix
// Use nonstd::string's split():
#include "nonstd/string.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
template< typename T >
std::string contents(std::vector<T> const & coll)
{
// using nonstd::to_string() for nonstd::std17::string_view:
using nonstd::to_string;
std::stringstream os;
for ( auto const & elem : coll )
os << "'" << to_string(elem) << "', ";
return os.str();
}
template< typename T >
std::ostream & operator<<(std::ostream & os, std::vector<T> const & coll )
{
return os << "[" << contents(coll) << "]";
}
int main()
{
std::cout << nonstd::string::split("Hello, world", ", ");
}
prompt> g++ -std=c++11 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
['Hello', 'world', ]
string lite is a single-file header-only library to provide various string algorithms. Firstly meant to get you up and running easily, not necessarily to provide everything that might be useful and in the most efficient manner.
Creating string lite I've had a look at the C++ standard, Boost, Facebook Folly, the Python standard library, the proposal for std::split()
and several algorithms I created over time.
Features and properties of string lite are ease of installation (single header), freedom of dependencies other than the standard library.
string lite is distributed under the Boost Software License.
string lite has no other dependencies than the C++ standard library.
string lite is a single-file header-only library. Put string.hpp
in the include folder directly into the project source tree or somewhere reachable from your project.
Contents
Documentation of string lite
Configuration
The following table presents types, values and simplified, short prototypes of the functions in string-bare's nonstd
namespace.
Kind | Type or function | Notes |
---|---|---|
Type | [string::]std17::basic_string_view | std::basic_string_view for C++17 and later, polyfill otherwise |
[string::]std17::string_view | character variations as requested, see section Configuration | |
[string::]std17::wstring_view; | ||
[string::]std17::u8string_view; | ||
[string::]std17::u16string_view; | ||
[string::]std17::u32string_view; | ||
Value | size_t string::npos | not-found position value, in nonstd::string namespace |
Utility | string to_string(string_view sv) | string from local or C++17 std string_view |
Comparison | int compare(string_view lhs, string_view rhs) | negative, zero or positive for lsh is less than, equal to or greater than rhs |
bool operator==(string_view lhs, string_view rhs) | true if lhs string is equal to rhs string | |
bool operator!=(string_view lhs, string_view rhs) | true if lhs string is not equal to rhs string | |
bool operator<(string_view lhs, string_view rhs) | true if lhs string is less than rhs string | |
bool operator<=(string_view lhs, string_view rhs) | true if lhs string is less than or equal to rhs string | |
bool operator>=(string_view lhs, string_view rhs) | true if lhs string is greater than or equal to rhs string | |
bool operator>(string_view lhs, string_view rhs) | true if lhs string is greater than or equal to rhs string | |
Observer | size_t length(string_view sv) | length of string |
size_t size(string_view sv) | length of string | |
bool is_empty(string_view sv) | true if string is empty | |
contains | bool contains(string_view sv, string_view what) | true if string contains given string |
bool contains_all_of(string_view sv, string_view set) | true if string contains all characters of set | |
bool contains_any_of(string_view sv, string_view set) | true if string contains any character of set | |
bool contains_none_of(string_view sv, string_view set) | true if string contains no character of set | |
starts_with | bool starts_with(string_view sv, string_view what) | true if string starts with given string |
bool starts_with_all_of(string_view sv, string_view set) | true if string starts with all characters of set | |
bool starts_with_any_of(string_view sv, string_view set) | true if string starts with any character of set | |
bool starts_with_none_of(string_view sv, string_view set) | true if string starts with no character of set | |
ends_with | bool ends_with(string_view sv, string_view what) | true if string ends with given string |
bool ends_with_all_of(string_view sv, string_view set) | true if string ends with all characters of set | |
bool ends_with_any_of(string_view sv, string_view set) | true if string ends with any character of set | |
bool ends_with_none_of(string_view sv, string_view set) | true if string ends with no character of set | |
Searching | size_t find_first(string_view sv, string_view what) | position of first occurrence of given string, or npos |
size_t find_first_of(string_view sv, string_view set) | position of first occurrence of character in set, or npos | |
size_t find_first_not_of(string_view sv, string_view set) | position of first occurrence of character not in set, or npos | |
find | size_t find_last(string_view sv, string_view what) | position of last occurrence of given string, or npos |
size_t find_last_of(string_view sv, string_view set) | position of last occurrence of character in set, or npos | |
size_t find_last_not_of(string_view sv, string_view set) | position of last occurrence of character not in set, or npos | |
Modifier | char_t to_lowercase(char_t chr) | character transformed to lowercase |
char_t to_uppercase(char_t chr) | character transformed to uppercase | |
string to_lowercase(string_view sv) | string transformed to lowercase | |
string to_uppercase(string_view sv) | string transformed to uppercase | |
case | string capitalize(string_view sv) | string transformed to start with capital |
substring | string substring(string_view sv, size_t pos [, size_t count]) | substring starting at given position of given length, default up to end |
erase | string erase(string_view sv, size_t pos [, size_t count]) | string with substring at given position of given length removed, default up to end |
string erase_all(string_view sv, string_view what) | string with all occurrences of 'what' removed | |
string erase_first(string_view sv, string_view what) | string with first occurrence of 'what' removed | |
string erase_last(string_view sv, string_view what) | string with last occurrence of 'what' removed | |
insert | string insert(string_view sv, size_t pos, string_view what) | string with substring 'what' inserted at given position |
replace | string replace(string_view sv, size_t pos, size_t length, string_view with) | string with substring pos to pos+length replaced with 'with' |
string replace_all(string_view sv, string_view what, string_view with) | string with all occurrences of 'what' replaced with 'with' | |
string replace_first(string_view sv, string_view what, string_view with) | string with first occurrence of 'what' replaced with 'with' | |
string replace_last(string_view sv, string_view what, string_view with) | string with last occurrence of 'what' replaced with 'with' | |
strip | string strip(string_view sv [, string_view set]) | string with characters given in set stripped from left and right, default " \t\n" |
string strip_left(string_view sv [, string_view set]) | string with characters given in set stripped from left, default " \t\n" | |
string strip_right(string_view sv [, string_view set]) | string with characters given in set stripped from right, default " \t\n" | |
Combining | string append(string_view head, string_view tail) | string with tail appended to head |
string join(collection<string_view> vec, string_view sep) | string with elements of collection joined with given separator string | |
Separating | vector<string_view> split(string_view sv, string_view set [, Nsplit]) | vector of string_view with elements of string separated by characters from given set, default no limit on elements |
tuple<string_view, string_view> split_left(string_view sv, string_view set [, size_t count]) | tuple with head and tail string_view on given string as split at left by characters in given set, default all in set | |
tuple<string_view, string_view> split_right(string_view sv, string_view set [, size_t count]) | tuple with head and tail string_view on given string as split at right by characters in given set, default all in set |
If the compiler supports __has_include()
, string lite supports the tweak header mechanism. Provide your tweak header as nonstd/string.tweak.hpp
in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like #define string_CPLUSPLUS 201103L
.
-Dstring_CONFIG_PROVIDE_XXX_T=1
Define the character type to provide the string algorithms for.
string_CONFIG_PROVIDE_XXX_T
can be one or more of:
string_CONFIG_PROVIDE_CHAR_T
, default 1string_CONFIG_PROVIDE_WCHAR_T
, default 0string_CONFIG_PROVIDE_CHAR8_T
, default 0string_CONFIG_PROVIDE_CHAR16_T
, default 0string_CONFIG_PROVIDE_CHAR32_T
, default 0
TODO: regex function not yet available.
-Dstring_CONFIG_PROVIDE_REGEX=1
Define this to 0 if you want to compile without regular expressions. Default is 1
. Note that including regular expressions incurs significant compilation overhead.
-Dstring_CPLUSPLUS=199711L
Define this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the __cplusplus
macro correctly.
-Dstring_CONFIG_NO_EXCEPTIONS=0
Define this to 1 if you want to compile without exceptions. If not defined, the header tries and detect if exceptions have been disabled (e.g. via -fno-exceptions
). Default is undefined.
- n3593 - std::split(): An algorithm for splitting strings. / https://isocpp.org/files/papers/n3593.html
- Martin Broadhurst. Exploring String Splitting in C++ Programming. 2023.
- Search GitHub for C++ string algorithms.
In the test runner, the version of string-lite is available via tag [.version]
. The following tags are available for information on the compiler and on the C++ standard library used: [.compiler]
, [.stdc++]
, [.stdlanguage]
and [.stdlibrary]
.
click to expand
length: length of given string
size: length of given string
is_empty: true if string is empty
contains: true if string contains substring
contains_all_of: true if string contains all characters of set
contains_any_of: true if string contains any character of set
contains_none_of: true if string contains no character of set
starts_with: true if string starts with substring
starts_with_all_of: true if string starts with all characters of set
starts_with_any_of: true if string starts with any character of set
starts_with_none_of: true if string starts with no character of set
ends_with: true if string ends with substring
ends_with_all_of: true if string ends with all characters of set
ends_with_any_of: true if string ends with any character of set
ends_with_none_of: true if string ends with no character of set
find_first: position of first substring in string
find_last: position of last substring in string
find_first_of: position of first character in string in set
find_last_of: position of last character in string in set
find_first_not_of: position of first character in string not in set
find_last_not_of: position of last character in string not in set
capitalize: string transformed to start with capital
to_lowercase: char transformed to lowercase
to_lowercase: string transformed to lowercase
to_uppercase: char transformed to uppercase
to_uppercase: string transformed to uppercase
append: string with second string concatenated to first string
substring: substring starting at given position of given length, default up to end
erase: string with substring at given position of given length removed - default up to end
erase_all: string with all occurrences of substring removed
erase_first: string with first occurrence of substring removed
erase_last: string with last occurrence of substring removed
insert: string with substring inserted at given position
replace: string with substring given by position and length replaced
replace_all: string with all occurrences of substring replaced
replace_first: string with first occurrence of substring replaced
replace_last: string with last occurrence of substring replaced
strip_left: string with characters in set removed from left of string [" \t\n"]
strip_right: string with characters in set removed from right of string [" \t\n"]
strip: string with characters in set removed from left and right of string [" \t\n"]
join: string with strings from collection joined separated by given separator
split: split string into vector of string_view given set of delimiter characters
split_left: split string into two-element tuple given set of delimiter characters - forward
split_right: split string into two-element tuple given set of delimiter characters - reverse
compare: negative, zero or positive for lsh is less than, equal to or greater than rhs
operator==(): true if lhs string is equal to rhs string
operator!=(): true if lhs string is not equal to rhs string
operator<(): true if lhs string is less than rhs string
operator<=(): true if lhs string is less than or equal to rhs string
operator>=(): true if lhs string is greater than or equal to rhs string
operator>(): true if lhs string is greater than or equal to rhs string
tweak header: Reads tweak header if supported [tweak]