GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - mwkpe/string-utils: Basic utility functions using string_view

BranchesTags
Open more actions menu

Latest commit

 

History

45 Commits
45 Commits

Folders and files

NameName
Last commit message
Last commit date
bench
bench
 
 
test
test
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
string_utils.h
string_utils.h
 
 

Repository files navigation

string_utils

Utility functions for strings using on C++17 <string_view> and <charconv> to avoid unnecessary string copies.

Status

WIP, no guarantees, but useable for single byte encoded strings; obviously not Unicode aware or anything fancy.

Examples

using namespace nonstd::string_utils;

// Check
starts_with("hello world", "hello"); // true
ends_with("apple orange", "banana");  // false

// Split
auto view = split("hello,world", ",");  // Returns vector<string_view>
auto copy = split_copy("hello,world", ",");  // Returns vector<string>
// Empty parts are kept by default
auto a = split("123-456-", "-");  // a.size() == 3, a[0] == "123", a[1] == "456", a[2] empty
// Ignore empty parts
auto b = split("123-456-", "-", false);  // b.size() == 2, b[0] == "123", b[1] == "456"

// Grab
auto line = std::string{"<AzureDiamond> doesnt look like stars to me"};
auto message = after_first(line, "> ");  // message = "doesnt look like stars to me"
auto name = between(line, "<", ">");  // name = "AzureDiamond"

// Replace
auto r = replace("hello world", "hello", "goodbye");  // r = "goodbye world"

// Conversion (requires <charconv> implementation, e.g. GCC 8)
auto i = as_int("42");  // i = 42
// Floating point charconv not yet implemented by GCC 8.1, does a string copy for now >:(
auto f = as_float("13.37");  // f = 13.37

// Awesome
auto csv = std::string{"42,13.37,test"};
auto values = split(csv, ",");
auto i = as_uint8(values[0]);  // i = 42
auto f = as_float(values[1]);  // f = 13.37
auto s = as_string(values[2]); // s = "test"

// ASCII stuff
auto s1 = std::string{"abc"};
auto s2 = ascii::as_upper(s1);  // s1 == "abc", s2 == "ABC"
ascii::to_upper(s1);  // s1 == "ABC"

Test and benchmark

g++ -std=c++17 -O2 -Wall -o test test.cpp
g++ -std=c++17 -O2 -Wall -o bench bench.cpp

Using the benchmark requires the hayai header files in a bench/hayai subdirectory.

Acknowledgements

doctest and hayai for testing

About

Basic utility functions using string_view

Topics

cpp17stringstring-manipulationstring-view

Resources

Readme
MIT license
Activity

Stars

4 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.