string lite: string facilities for C++98 and later (In Progress)

Language License Build Status Build status Version download Conan Try it on wandbox Try it on godbolt online

Contents

Example usage

// 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 to_string() for nonstd::string::string_view:

    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", ",");
}

Compile and run

prompt> g++ -std=c++98 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
['Hello', ' world', ]

In a nutshell

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.

License

string lite is distributed under the Boost Software License.

Note: this repository contains a copy of several files from the CsString library by Ansel Sermersheim and Barbara Geller for testing purposes. The CsString library is released under the BSD 2-clause license.

Dependencies

string lite has no other dependencies than the C++ standard library.

Installation and use

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.

Synopsis

Contents
Documentation of string lite
Configuration

Documentation of string lite

Kind Type or function Notes
Type literal_delimiter a single string
  any_of_delimiter any of given characters
  fixed_delimiter fixed length
  limit_delimiter not implemented
  regex_delimiter regular expression
  char_delimiter character position
     
Utilities CharT nullchr() null char of template type
     
  size_t size(CharT * s) C-string, [w,u16,u32]char
  size_t size(CollT & c) collection, C++ string
     
  CharT * begin(CharT * c) iterator to C-string
  CharT * end(CharT * c) iterator past C-string
  CharT const * cbegin(CharT * c) const iterator to C-string
  CharT const * cend(CharT * c) const iterator past C-string
     
  IterT begin(CollT & c) collection, C++ string
  IterT end(CollT & c) collection, C++ string
  IterT cbegin(CollT & c) collection, C++ string
  IterT cend(CollT & c) collection, C++ string
  IterT rbegin(CollT & c) collection, C++ string
  IterT rend(CollT & c) collection, C++ string
  IterT crbegin(CollT & c) collection, C++ string
  IterT crend(CollT & c) collection, C++ string
     
  std::basic_string<>
to_string(basic_string_view<> v)
 
  std::basic_string<>
to_string(basic_string_view<> v, Allocator const & a)
 
     
  CharT const *
to_identity(CharT const * s)
 
  basic_string<>
to_identity(basic_string<> const & s)
 
  basic_string_view<>
to_identity(basic_string_view<> v)
std::string_view
  basic_string<>
to_identity(basic_string_view<> v)
nonstd::string_view
     
Observers bool is_empty(CharT * s) C-string is empty
  bool is_empty(StringT const & s) string is empty
     
  bool contains(StringT const & s, CharT chr) string contains chr
  bool contains(StringT const & s, SubT const & substr) string contains substring
  bool contains(StringT const & s, std::regex const & substr) string contains reg. expr.
  bool contains_re(StringT const & s, ReT const & re) string contains reg. expr.
     
  bool starts_with(StringT const & s, CharT chr) string starts with chr
  bool starts_with(StringT const & s, SubT const & substr) string starts with substring
     
  bool ends_with(StringT const & s, CharT chr) string ends with chr
  bool ends_with(StringT const & s, SubT const & substr) string ends with substring
     
Searching IterT find_first(StringT & s, SubT const & substr) iterator to substring
  IterT find_first(StringT const & s, SubT const & substr) const iterator to substring
     
  IterT find_last(StringT & s, SubT const & substr) iterator to substring
  IterT find_last(StringT const & s, SubT const & substr) const iterator to substring
     
Modifiers CharT * clear(CharT * s) make C-string empty
  StringT & clear(StringT & s) make string empty
     
  CharT * to_lowercase(CharT * p) convert C-string to lowercase
  CharT * to_uppercase(CharT * p) convert C-string to uppercase
  StringT & to_lowercase(StringT & s) convert string to lowercase
  StringT & to_uppercase(StringT & s) convert string to uppercase
     
  StringT as_lowercase(StringT const & s) string converted to lowercase
  StringT as_uppercase(StringT const & s) string converted to uppercase
     
  StringT & replace_all(StringT & s, FromT const & from, ToT const & to)  
  StringT replaced_all(StringT const & s, FromT const & from, ToT const & to)  
     
  StringT & replace_first(StringT & s, FromT const & from, ToT const & to)  
  StringT replaced_first(StringT const & s, FromT const & from, ToT const & to)  
     
  StringT & replace_last(StringT & s, FromT const & from, ToT const & to)  
  StringT replaced_last(StringT const & s, FromT const & from, ToT const & to)  
     
  CharT * trim_left(CharT * s) [" \t\n"]
  CharT * trim_left(CharT * s, SetT const * set)  
  StringT & trim_left(StringT & s)  
  StringT & trim_left(StringT & s, SetT const & set)  
     
  CharT * trim_right(CharT * s)  
  CharT * trim_right(CharT * s, SetT const * set)  
  StringT & trim_right(StringT & s)  
  StringT & trim_right(StringT & s, SetT const & set)  
     
  CharT * trim(CharT * s)  
  CharT * trim(CharT * s, SetT const * set)  
  StringT & trim(StringT & s)  
  StringT & trim(StringT & s, SetT const & set)  
     
  StringT trimmed_left(StringT const & s)  
  StringT trimmed_left(StringT const & s, SetT const & set)  
     
  StringT trimmed_right(StringT const & s)  
  StringT trimmed_right(StringT const & s, SetT const & set)  
     
  StringT trimmed(StringT const & s)  
  StringT trimmed(StringT const & s, SetT const & set)  
     
Combining CharT * append(CharT * s, TailT const & tail)  
  StringT & append(StringT & s, TailT const & tail)  
  StringT appended(StringT const & s, TailT const & tail)  
     
  StringT join(Coll const & coll, SepT const & sep)  
  std::vector<SViewT> split(SViewT text, Delimiter d) See delimiter types
  std::vector<SViewT> split(SViewT text, char const * d)  

Note: with StringT const & the string type can also be string_view.

Configuration

Tweak header

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.

Select internal string_view, nonstd::string_view or std::string_view

At default, string lite uses an internal string_view. You can however override this default and explicitly request to use string-view lite's nonstd::string_view as nonstd::string::string_view or use C++17's std::string_view via the following macros.

-Dstring_CONFIG_SELECT_STRING_VIEW=string_CONFIG_SELECT_STRING_VIEW_INTERNAL Define this to string_CONFIG_SELECT_STRING_VIEW_NONSTD to select nonstd::string_view as nonstd::string::string_view. Define this to string_CONFIG_SELECT_STRING_VIEW_STD to select std::string_view as nonstd::string::string_view. Default is undefined, which has the same effect as defining to string_CONFIG_SELECT_STRING_VIEW_INTERNAL.

Standard selection macro

-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.

Disable exceptions

-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.

Notes and references

TODO

Appendix

A.1 Compile-time information

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].

A.2 string-lite test specification

click to expand

string: Setting Windows console to print utf8 characters[unicode][windows]
is_empty: true if string is empty - char *
is_empty: true if string is empty - string
contains: true if string contains sub string - string-char
contains: true if string contains sub string - string-char*
contains: true if string contains sub string - string-string
contains: true if string contains sub string - string-string_view
contains: true if string contains sub string - string_view-string_view
contains: true if string contains regular expression - string-std::regexp
contains_re: true if string contains regular expression - string-char*
contains_re: true if string contains regular expression - string-string
starts_with: true if string starts with sub string - string-char
starts_with: true if string starts with sub string - string-char*
starts_with: true if string starts with sub string - string-string
starts_with: true if string starts with sub string - string-string_view
starts_with: true if string starts with sub string - string_view-string_view
ends_with: true if string ends with sub string - string-char
ends_with: true if string ends with sub string - string-char*
ends_with: true if string ends with sub string - string-string
ends_with: true if string ends with sub string - string-string_view
ends_with: true if string ends with sub string - string_view-string_view
find_first: iterator to sub string in string - string-char*
find_first: iterator to sub string in string - string-string
find_first: iterator to sub string in string - string-string_view
find_first: iterator to sub string in string_view - string_view-string_view
find_last: iterator to sub string in string - string-char*
find_last: iterator to sub string in string - string-string
find_last: iterator to sub string in string - string-string_view
find_last: iterator to sub string in string_view - string_view-string_view
clear: Makes string empty - char *
clear: Makes string empty - string
replace_all: Change all occurrences of sub string - string-char*
replace_all: Change all occurrences of sub string - string-string
replace_all: Change all occurrences of sub string - string-string_view
replaced_all: Return new string with all occurrences of sub string changed - char*-char*
replaced_all: Return new string with all occurrences of sub string changed - string-string
replaced_all: Return new string with all occurrences of sub string changed - string-string_view
replaced_all: Return new string with all occurrences of sub string changed - string_view-string_view [TODO]
replace_first: Change the first occurrence of sub string - char*-char*[TODO]
replace_first: Change the first occurrence of sub string - string-char*
replace_first: Change the first occurrence of sub string - string-string
replace_first: Change the first occurrence of sub string - string-string_view
replace_first: Change the first occurrence of sub string - string_view-string_view
replaced_first: Return new string with first occurrence of sub string changed - char*-char*
replaced_first: Return new string with first occurrence of sub string changed - string-string
replaced_first: Return new string with first occurrence of sub string changed - string-string_view
replaced_first: Return new string with first occurrence of sub string changed - string_view-string_view [TODO]
replace_last: Change the first occurrence of sub string - char*-char*[TODO]
replace_last: Change the last occurrence of sub string - string-char*
replace_last: Change the last occurrence of sub string - string-string
replace_last: Change the last occurrence of sub string - string-string_view
replace_last: Change the last occurrence of sub string - string_view-string_view
replaced_last: Return new string with last occurrence of sub string changed - char*-char*
replaced_last: Return new string with last occurrence of sub string changed - string-string
replaced_last: Return new string with last occurrence of sub string changed - string-string_view
replaced_last: Return new string with last occurrence of sub string changed - string_view-string_view [TODO]
to_lowercase: Makes string lowercase - char *
to_uppercase: Makes string uppercase - char *
to_lowercase: Makes string lowercase - string
to_uppercase: Makes string uppercase - string
as_lowercase: Return new string in lowercase - string
as_uppercase: Return new string in uppercase - string
append: Append a string to a string in-place - char*-char* - Note: be careful!
append: Append a string to a string in-place - string-char*
append: Append a string to a string in-place - string-string
append: Append a string to a string in-place - string-string_view
appended: Return new string with second string appended to first string - string-char*
appended: Return new string with second string appended to first string - string-string
appended: Return new string with second string appended to first string - string-string_view
trim_left: Remove characters in set from left of string [" \t\n"] - in-place - C-string
trim_left: Remove characters in set from left of string [" \t\n"] - in-place - std::string
trim_right: Remove characters in set from right of string [" \t\n"] - in-place - char*
trim_right: Remove characters in set from right of string [" \t\n"] - in-place - string
trim: Remove characters in set from left and right of string [" \t\n"] - in-place - char*
trim: Remove characters in set from left and right of string [" \t\n"] - in-place - string
trimmed_left: Remove characters in set from left of string [" \t\n"] - copy - string
trimmed_right: Remove characters in set from right of string [" \t\n"] - copy - string
trimmed: Remove characters in set from left and right of string [" \t\n"] - copy - string
string_view: ...[TODO]
join: Join strings from collection into a string separated by given separator
split: Split string into vector of string_view given delimiter - literal_delimiter
split: Split string into vector of string_view given delimiter - literal_delimiter
split: Split string into vector of string_view given delimiter - literal_delimiter
split: Split string into vector of string_view given delimiter - literal_delimiter
split: Split string into vector of string_view given delimiter - literal_delimiter
split: Split string into vector of string_view given delimiter - any_of_delimiter
split: Split string into vector of string_view given delimiter - fixed_delimiter
split: Split string into vector of string_view given delimiter - regex_delimiter
split: Split string into vector of string_view given delimiter - char_delimiter
split: Split string into single characters given empty delimiter
clear: Makes string empty - string [unicode]
tweak header: Reads tweak header if supported [tweak]

String Lite

String facilities for C++98 and later

String Lite Info

⭐ Stars 12
🔗 Source Code github.com
🕒 Last Update 4 months ago
🕒 Created a year ago
🐞 Open Issues 1
➗ Star-Issue Ratio 12
😎 Author martinmoene