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 - yurablok/cpp-string-utils: C++17 and C++11 std::string-and-std::string_view-based utils.

BranchesTags
Open more actions menu

Latest commit

 

History

15 Commits
15 Commits

Folders and files

NameName
Last commit message
Last commit date
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
string_utils.hpp
string_utils.hpp
 
 

Repository files navigation

C++ String Utils

C++17 and C++11 std::string-and-std::string_view-based UTF-8-based utils.
https://utf8everywhere.org/

to_string

u8string buffer(32, '\0');
u8string_view result = utils::to_string(12.34f, buffer);
assert(!result.empty());
assert(result == "12.34");
u8string buffer(32, '\0');
u8string_view result = utils::to_string(0xDEADBEEF, buffer, true);
assert(!result.empty());
assert(result == "deadbeef");

from_string

float f32 = 0.0f;
assert(utils::from_string("12.34", f32));
assert(f32 == 12.34f);
uint32_t u32 = 0;
assert(utils::from_string("DEADBEEF", u32, true));
assert(u32 == 0xDEADBEEF);
assert(utils::from_string("deadbeef", u32, true));
assert(u32 == 0xDEADBEEF);

trimm

assert(u8string_view("\n 12.34 \t").trim() == "12.34")
assert(u8string_view(u8" | 🌍 | ").trim("|") == u8"🌍");
assert(u8string_view(u8"<span\>>πŸ˜€</span>").trim('<', '>') == u8"πŸ˜€");

split

u8string_view(u8"|12||🌍|34|5\\|6|").split("|", [](u8string_view part, uint32_t idx) {
    switch (idx) {
    case 0: assert(part == "12"); break;
    case 1: assert(part == u8"🌍"); break;
    case 2: assert(part == "34"); break;
    case 3: assert(part == "5\\|6"); break;
    default: assert(false); break;
    }
});

std::array<u8string_view, 4> parts;
u8string_view(u8"|12||🌍|34|5\\|6|").split("|", parts);
assert(parts[0] == "12");
assert(parts[1] == u8"🌍");
assert(parts[2] == "34");
assert(parts[3] == "5\\|6");

slice

u8string_view u8strv = "ABCDEFGHIJ";
assert(u8strv.slice(2, 6) == "CDEF");
assert(u8strv.slice(-3) == "HIJ");
assert(u8strv.slice(0, -2) == "ABCDEFGH");
assert(u8strv.slice(-5, -2) == "FGH");

u8strv = u8"A🌍CΠ”EΠ€GHπŸ˜€J";
assert(u8strv.slice_cp(2, 6) == u8"CΠ”EΠ€");
assert(u8strv.slice_cp(-3) == u8"HπŸ˜€J");
assert(u8strv.slice_cp(0, -2) == u8"A🌍CΠ”EΠ€GH");
assert(u8strv.slice_cp(-5, -2) == u8"Π€GH");

substr

constexpr u8string_view str = "[email protected]";
size_t offset = 0;
u8string_view user = utils::substr(str, offset, "@");
assert(user == "user");
u8string_view email = utils::substr(str, offset, ".");
assert(email == "email");
u8string_view com = utils::substr(str, offset, ".");
assert(com == "com");

conversion

u8string u8str;
u16string u16str;
u32string u32str;

u8str = u8"🌍";
assert(u8str.size_B() == 4);
assert(u8str.size_cu() == 4);
assert(u8str.size_cp() == 1);

u8str = u8"δ½ ε₯½ Hola Hello OlΓ‘ πŸ˜€ ΠŸΡ€ΠΈΠ²Π΅Ρ‚ こんにけは μ•ˆλ…•ν•˜μ„Έμš” Bonjour Hallo Ciao";
assert(u8str.size_B() == 91);
assert(u8str.size_cu() == 91);
assert(u8str.size_cp() == 57);

u16str = u8str.toUtf16();
assert(u16str.size_B() == 116);
assert(u16str.size_cu() == 58);
assert(u16str.size_cp() == 57);
assert(u16str.toUtf8() == u8str);
assert(u16str.toUtf32().toUtf8() == u8str);

u32str = u8str.toUtf32();
assert(u32str.size_B() == 228);
assert(u32str.size_cu() == 57);
assert(u32str.size_cp() == 57);
assert(u32str.toUtf8() == u8str);
assert(u32str.toUtf16().toUtf8() == u8str);

std::string-compatibility

std::map<u8string, int32_t> map;
std::map<u8string_view, int32_t> map;
std::unordered_map<u8string, int32_t> map;
std::unordered_map<u8string_view, int32_t> map;

std::regex re(u8string(u8"Γ€+"));
assert(std::regex_search(u8string(u8"Γ€bc"), re));
assert(not std::regex_search("abc", re));

etc...

About

C++17 and C++11 std::string-and-std::string_view-based utils.

Topics

cpp11cpp17from-stringsplitstring-viewsubstrto-stringtrimmutils

Resources

Readme
BSL-1.0 license
Activity

Stars

2 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

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.