Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
strfnd.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <string>
8#include <string_view>
9
10// FIXME: convert this class to string_view
11template <typename T>
13 typedef std::basic_string<T> String;
15 size_t pos;
16public:
17 BasicStrfnd(const String &s) { start(s); }
18 BasicStrfnd(const T *ptr) { str = ptr; pos = 0; }
19 BasicStrfnd(std::basic_string_view<T> sv) { str = sv; pos = 0; }
20
21 void start(const String &s) { str = s; pos = 0; }
22 size_t where() { return pos; }
23 void to(size_t i) { pos = i; }
24 bool at_end() { return pos >= str.size(); }
25 String what() { return str; }
26
27 String next(const String &sep)
28 {
29 if (pos >= str.size())
30 return String();
31
32 size_t n;
33 if (sep.empty() || (n = str.find(sep, pos)) == String::npos) {
34 n = str.size();
35 }
36 String ret = str.substr(pos, n - pos);
37 pos = n + sep.size();
38 return ret;
39 }
40
41 // Returns substr up to the next occurrence of sep that isn't escaped with esc ('\\')
42 String next_esc(const String &sep, T esc=static_cast<T>('\\'))
43 {
44 if (pos >= str.size())
45 return String();
46
47 size_t n, old_p = pos;
48 do {
49 if (sep.empty() || (n = str.find(sep, pos)) == String::npos) {
50 pos = n = str.size();
51 break;
52 }
53 pos = n + sep.length();
54 } while (n > 0 && str[n - 1] == esc);
55
56 return str.substr(old_p, n - old_p);
57 }
58
59 void skip_over(const String &chars)
60 {
61 size_t p = str.find_first_not_of(chars, pos);
62 if (p != String::npos)
63 pos = p;
64 }
65};
66
Definition strfnd.h:12
bool at_end()
Definition strfnd.h:24
String next(const String &sep)
Definition strfnd.h:27
void skip_over(const String &chars)
Definition strfnd.h:59
void to(size_t i)
Definition strfnd.h:23
BasicStrfnd(const String &s)
Definition strfnd.h:17
size_t where()
Definition strfnd.h:22
String str
Definition strfnd.h:14
String next_esc(const String &sep, T esc=static_cast< T >('\\'))
Definition strfnd.h:42
std::basic_string< T > String
Definition strfnd.h:13
BasicStrfnd(std::basic_string_view< T > sv)
Definition strfnd.h:19
BasicStrfnd(const T *ptr)
Definition strfnd.h:18
void start(const String &s)
Definition strfnd.h:21
String what()
Definition strfnd.h:25
size_t pos
Definition strfnd.h:15
BasicStrfnd< char > Strfnd
Definition strfnd.h:67
BasicStrfnd< wchar_t > WStrfnd
Definition strfnd.h:68
static std::string p(std::string path)
Definition test_filesys.cpp:53