From 5bd042ce011182056ab612141ca77303d4c66b76 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 21 Aug 2017 19:04:39 +0200 Subject: [PATCH] Implement split() for std::string. This is the beginning of the removal of lol::String which is not really useful once all its tiny utility functions are implemented for std::string. --- src/base/string.cpp | 33 +++++++++++++++++++++++++++++++++ src/lol/base/string.h | 27 ++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/base/string.cpp b/src/base/string.cpp index 3a7e1fca..29c5f7b7 100644 --- a/src/base/string.cpp +++ b/src/base/string.cpp @@ -25,6 +25,39 @@ namespace lol { +array split(std::string const &s, char sep) +{ + array ret; + size_t start = 0, end = 0; + while ((end = s.find(sep, start)) != std::string::npos) + { + ret << s.substr(start, end - start); + start = end + 1; + } + ret << s.substr(start); + return ret; +} + +array split(std::string const &s, std::string const &seps) +{ + array ret; + size_t start = s.find_first_not_of(seps), end = 0; + + while ((end = s.find_first_of(seps, start)) != std::string::npos) + { + ret << s.substr(start, end - start); + start = s.find_first_not_of(seps, end); + } + if (start != std::string::npos) + ret << s.substr(start); + + return ret; +} + +/* + * XXX: deprecated + */ + String String::format(char const *format, ...) { String ret; diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 93145242..3f2afaf2 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // © 2013—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -14,9 +14,30 @@ #pragma once // -// The String class +// The string tools // ---------------- -// A very simple String class, based on Array. +// Contains some utilities to work with std::string objects. +// + +#include + +namespace lol +{ + +/* Split a string along a single separator */ +array split(std::string const &s, char sep); + +/* Split a string along multiple separators */ +array split(std::string const &s, std::string const &seps); + +} /* namespace lol */ + +// +// The deprecated String class +// --------------------------- +// A very simple String class, based on Array. The most interesting +// thing in there was LOL_ATTR_NODISCARD but apart from that there was +// no real point in using our own class. Phase this out. // #include