Browse Source

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.
legacy
Sam Hocevar 7 years ago
parent
commit
5bd042ce01
2 changed files with 57 additions and 3 deletions
  1. +33
    -0
      src/base/string.cpp
  2. +24
    -3
      src/lol/base/string.h

+ 33
- 0
src/base/string.cpp View File

@@ -25,6 +25,39 @@
namespace lol
{

array<std::string> split(std::string const &s, char sep)
{
array<std::string> 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<std::string> split(std::string const &s, std::string const &seps)
{
array<std::string> 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;


+ 24
- 3
src/lol/base/string.h View File

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2017 Sam Hocevar <sam@hocevar.net>
// © 2013—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// 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 <string>

namespace lol
{

/* Split a string along a single separator */
array<std::string> split(std::string const &s, char sep);

/* Split a string along multiple separators */
array<std::string> 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 <lol/base/assert.h>


Loading…
Cancel
Save