sources/mystring.cpp

Thu, 28 Jan 2021 11:39:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 28 Jan 2021 11:39:48 +0200
branch
protocol5
changeset 199
927f4b8ef6f7
parent 197
819fdef70d68
permissions
-rw-r--r--

merged with default

1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
190
90bf9049e5eb updated copyright year
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
2 Copyright 2014 - 2021 Teemu Piippo
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 All rights reserved.
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 are met:
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 1. Redistributions of source code must retain the above copyright
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 contributors may be used to endorse or promote products derived from
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 this software without specific prior written permission.
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 */
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include <cstring>
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include "main.h"
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "mystring.h"
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
35 BEGIN_ZFC_NAMESPACE
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
36
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
37 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
38 * \brief Converts the given string to lowercase.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
39 * \param string string to convert
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
40 * \return converted string
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
41 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
42 std::string to_lowercase(const std::string& string)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 {
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
44 std::string result = string;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
46 for (char& ch : result)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 {
137
485cb6d6b98c Made Range not include the 'max' parameter, replaced a lot of for()-loops with C++11-style range-for-loops
Teemu Piippo <teemu@compsta2.com>
parents: 129
diff changeset
48 if (isupper(ch))
485cb6d6b98c Made Range not include the 'max' parameter, replaced a lot of for()-loops with C++11-style range-for-loops
Teemu Piippo <teemu@compsta2.com>
parents: 129
diff changeset
49 ch += 'a' - 'A';
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
52 return result;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
55 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
56 * \brief Joins a list of strings to a single string.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
57 * \param strings strings to join together
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
58 * \param delimeter delimeter to join the strings together with
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
59 * \return joined string
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
60 */
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
61 std::string join_string_list(const std::vector<std::string>& strings, const std::string& delimeter)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
63 std::string result;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
65 for (const std::string &item : strings)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
66 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
67 if (not result.empty())
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
68 result += delimeter;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
70 result += item;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
73 return result;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
76 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
77 * \brief Extracts a substring from the middle of the given string
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
78 * \param str string to extract from
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
79 * \param rangeBegin starting index of the substring
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
80 * \param rangeEnd end index of the substring
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
81 * \return substring
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
82 */
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
83 std::string mid(const std::string& str, int rangeBegin, int rangeEnd)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 {
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
85 rangeBegin = max(rangeBegin, 0);
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
86 rangeEnd = min(rangeEnd, static_cast<signed>(str.length()));
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
87 std::string result;
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
88 if (rangeEnd > rangeBegin)
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
89 {
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
90 result = str.substr(rangeBegin, rangeEnd - rangeBegin);
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
91 }
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
92 return result;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
95 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
96 * \brief right Extracts a substring from the end of the substring
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
97 * \param str string to extract from
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
98 * \param length desired length of the new substring
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
99 * \return substring
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
100 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
101 std::string right(const std::string& str, int length)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
103 if (length >= static_cast<signed>(str.length()))
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
104 return str;
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
105 else
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
106 return std::string{str.data() + str.length() - length};
83
08bfc3d9d2ae Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
107 }
08bfc3d9d2ae Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
108
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
109 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
110 * \brief Formats a string using \c vsnprintf, using a va_list argument list
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
111 * \param formatString formatting string
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
112 * \param args argument list
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
113 * \return formatted string
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
114 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
115 std::string vsprintf(const char* formatString, va_list args)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
117 std::string result;
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
118 // Copy the argument list so that we have something to provide to vsnprintf in case we have to call it again.
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
119 va_list argsCopy;
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
120 va_copy(argsCopy, args);
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
121 // First, attempt to format using a fixed-size buffer.
149
7643c21d546b Restored the String::vsprintf buffer to 1024 characters, it only was 64 characters long for testing.
Teemu Piippo <teemu@compsta2.com>
parents: 148
diff changeset
122 static char buffer[1024];
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
123 const std::size_t length = std::vsnprintf(buffer, countof(buffer), formatString, args);
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
124 if (length < sizeof buffer)
69
eb4c25284a19 Removed a lot of boilerplate code
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
125 {
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
126 // vsnprintf succeeded in fitting the formatted string into the buffer, so we're done.
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
127 result = buffer;
69
eb4c25284a19 Removed a lot of boilerplate code
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
128 }
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
129 else
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
130 {
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
131 // vsnprintf needs more space, so we have to allocate a new buffer and try again.
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
132 result.resize(length + 1);
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
133 std::vsnprintf(result.data(), length + 1, formatString, argsCopy);
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
134 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
135 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
136 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
137
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
138 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
139 * \brief Formats a string using \c printf -like syntax
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
140 * \param formatString formatting string
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
141 * \param ... printf-like arguments
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
142 * \return formatted string
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
143 */
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
144 std::string __cdecl sprintf(const char* formatString, ...)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
145 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
146 va_list args;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
147 va_start (args, formatString);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
148 std::string result = vsprintf(formatString, args);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
149 va_end (args);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
150 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
151 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
152
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
153 /*!
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
154 * \brief Removes a substring from the middle of a string
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
155 * \param string original string
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
156 * \param start start index to remove
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
157 * \param end end index to remove
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
158 * \return modified string
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
159 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
160 std::string remove_range(const std::string &string, int start, int end)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
161 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
162 std::string result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
163 result.reserve(string.length() - (end - start));
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
164 std::copy(string.begin(), string.begin() + start, std::back_inserter(result));
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
165 std::copy(string.begin() + end, string.end(), std::back_inserter(result));
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
166 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
167 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
168
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
169 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
170 * \brief Finds out whether the specified string starts with the specified substring.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
171 * \param string string to test
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
172 * \param substring substring to look for
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
173 * \return bool
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
174 */
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
175 bool starts_with(const std::string& string, const std::string& substring)
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
176 {
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
177 if (string.length() < substring.length())
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
178 return false;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
179 else
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
180 return std::strncmp(string.data(), substring.data(), substring.length()) == 0;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
181 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
183 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
184 * \brief Replaces all instances of \c text with \c replacement in place.
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
185 * \param text Text to replace away.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
186 * \param replacement Text to replace \c text with.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
187 */
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
188 void replace_all(std::string& str, const char* text, const char* replacement)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
189 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
190 int position;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
191 while ((position = str.find(text)) != -1)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
192 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
193 str.replace(position, std::strlen(text), replacement);
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
194 }
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
197 /*!
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
198 * \brief Splits this string using the provided delimeter.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
199 * \param delimeter Delimeter to use for splitting.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
200 * \returns a string list containing the split strings.
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
201 */
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
202 std::vector<std::string> split(const std::string& string, const std::string& delimeter)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 {
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
204 std::vector<std::string> result;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
205 int a = 0;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
206 int b;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
208 // Find all separators and store the text left to them.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
209 while ((b = string.find(delimeter, a)) != -1)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 {
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
211 std::string sub = mid(string, a, b);
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
212
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
213 if (sub.length() > 0)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
214 result.push_back(sub);
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
216 a = b + delimeter.length();
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
219 // Add the string at the right of the last separator
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
220 if (a < static_cast<int>(string.length()))
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
221 result.push_back(mid(string, a, string.length()));
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
222
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 return result;
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
226 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
227 * \brief Attempts to convert the specified string to an integer.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
228 * \param str string to convert
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
229 * \param base numeric base to convert using.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
230 * \return the integer if conversion is successful.
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
231 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
232 std::optional<long> to_int(const char* str, int base)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
234 errno = 0;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
235 char* endPointer;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
236 long result = strtol(str, &endPointer, base);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
237 if (errno == 0 and *endPointer == '\0')
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
238 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
239 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
240 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
241 else
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
242 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
243 return {};
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
244 }
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
245 }
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
246
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
247 /*!
196
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
248 * \brief Removes unwanted characters from the specified string. Modification is done in place.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
249 * \param string String to modify.
58d4a48f0904 updated the documentation of the string functions
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
250 * \param filter The filtering function to use, defaults to std::isspace in order to remove whitespace.
147
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
251 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
252 void normalize(std::string& string, int (*filter)(int))
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
253 {
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
254 int a = 0;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
255 int b = string.length() - 1;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
256 while ((*filter)(string[a]) and a != b)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
257 {
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
258 ++a;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
259 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
260 while ((*filter)(string[b]) and a != b)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
261 {
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
262 --b;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
263 }
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
264 if (a == b)
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
265 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
266 string = "";
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
267 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
268 else if (a != 0 or b != static_cast<signed>(string.length() - 1))
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
269 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
270 string = string.substr (a, b - a + 1);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
271 }
107
ca10837a2a9e Backport string changes from protocol5
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
272 }
ca10837a2a9e Backport string changes from protocol5
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
273
105
b4466472aecd Added some basic IRC-like commands that can be used to do what keystrokes can do without actually using keystrokes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 88
diff changeset
274 END_ZFC_NAMESPACE

mercurial