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::regexand 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. The in-place modification class of functions is decidedly absent.
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.
In general, functions take string_views 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_views.
Note 1: to support use of string_views with C++ versions earlier than C++17, string_views may be accessed as nonstd::string::std17::string_view, supplying a local string_view class or std::string_view if present.
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-lite's nonstd namespace.
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]
