sources/mystring.cpp

Wed, 27 Jan 2021 18:55:03 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 18:55:03 +0200
changeset 186
9330b93d9946
parent 183
9b6a0daedfc0
child 190
90bf9049e5eb
permissions
-rw-r--r--

cleanup ip address code

1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
109
e4966d7e615d Happy new year 2016
Teemu Piippo <crimsondusk64@gmail.com>
parents: 107
diff changeset
2 Copyright 2014 - 2016 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 /*!
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
38 * \returns a lower-case version of this string.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
39 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
40 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
41 {
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
42 std::string result = string;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
44 for (char& ch : result)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 {
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
46 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
47 ch += 'a' - 'A';
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 83
diff changeset
50 return result;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
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
53 /*!
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
54 * \brief Joins the elements of this string list into one longer string.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
55 * \param delimeter The delimeter to place between the element strings.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
56 * \returns the catenated 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
57 */
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
58 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
59 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
60 std::string result;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
62 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
63 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
64 if (not result.empty())
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
65 result += delimeter;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
67 result += item;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
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
70 return result;
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 /*!
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
74 * \brief Modifies the given index so that if it is negative, it is translated into a positive index starting from the
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
75 * end of the string. For example, an index of -1 will be modified to point to the last character in the string,
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
76 * -2 to the second last, etc.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
77 * \param index Index to translate.
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
78 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
79 inline void modifyIndex(const std::string& str, int& index)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
81 if (index < 0)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
82 index = str.length() - index;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
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
85 /*!
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
86 * \param a Starting index of the range.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
87 * \param b Ending index of the range.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
88 * \returns a sub-string containing all characters from \c a to \c b, not including the character at \c b.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
89 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
90 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
91 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
92 modifyIndex(str, rangeBegin);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
93 modifyIndex(str, rangeEnd);
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
94 rangeBegin = max(rangeBegin, 0);
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
95 rangeEnd = min(rangeEnd, static_cast<signed>(str.length()));
129
a556ce001e26 Adjusted String::mid again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
96
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
97 if (rangeEnd <= rangeBegin)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 return "";
129
a556ce001e26 Adjusted String::mid again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 110
diff changeset
99 else
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
100 return str.substr(rangeBegin, rangeEnd - rangeBegin);
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
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
103 /*!
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
104 * \param length Amount of characters to return.
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 * \returns the \c length right-most characters of the string.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
106 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
107 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
108 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
109 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
110 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
111 else
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
112 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
113 }
08bfc3d9d2ae Removed format.cpp and format.h, use built-in formatting instead
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
114
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
115 /*!
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
116 * \brief Formats this string using \c vsnprintf, using the provided arguments.
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
117 * \param formatString Template string to use with formatting.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
118 * \param args Variadic arguments to use with formatting.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
119 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
120 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
121 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
122 std::string result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
123
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
124 // 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
125 va_list argsCopy;
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
126 va_copy(argsCopy, args);
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
128 // 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
129 static char buffer[1024];
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
130 size_t length = vsnprintf(buffer, sizeof buffer, formatString, args);
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
131
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
132 if (length < sizeof buffer)
69
eb4c25284a19 Removed a lot of boilerplate code
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
133 {
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
134 // 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
135 result = buffer;
69
eb4c25284a19 Removed a lot of boilerplate code
Teemu Piippo <crimsondusk64@gmail.com>
parents: 66
diff changeset
136 }
148
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
137 else
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
138 {
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
139 // vsnprintf needs more space, so we have to allocate a new buffer and try again.
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
140 Vector<char> newBuffer(length + 1);
19e98695e584 Made String::vsprintf behave properly with long strings.
Teemu Piippo <teemu@compsta2.com>
parents: 147
diff changeset
141 vsnprintf(newBuffer.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
142 result = newBuffer.data();
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
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
145 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
146 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
147
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
148 /*!
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
149 * \brief Formats this string using \c printf -like syntax.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
150 * \param formatString Template string to use with formatting.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
151 * \param ... Variadic arguments to use with formatting.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
152 */
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
153 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
154 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
155 va_list args;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
156 va_start (args, formatString);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
157 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
158 va_end (args);
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
159 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
160 }
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 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
163 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
164 std::string result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
165 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
166 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
167 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
168 return result;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
169 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
170
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
171
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
172 /*!
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
173 * \param other Sub-string to find from the beginning of this string.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
174 * \returns whether or not this string begins with the provided sub-string.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
175 */
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
176 bool starts_with(const std::string& str, const std::string& other)
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
177 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
178 if (str.length() < other.length())
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
179 return false;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
180 else
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
181 return std::strncmp(str.data(), other.data(), other.length()) == 0;
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
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
184 /*!
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
185 * \brief Replaces all instances of \c text with \c replacement.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
186 * \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
187 * \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
188 */
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
189 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
190 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
191 int position;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
192 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
193 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
194 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
195 }
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
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
198 /*!
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
199 * \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
200 * \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
201 * \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
202 */
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
203 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
204 {
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
205 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
206 int a = 0;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
207 int b;
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
209 // 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
210 while ((b = string.find(delimeter, a)) != -1)
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 {
183
9b6a0daedfc0 renamed String and StringList uses
Teemu Piippo <teemu@hecknology.net>
parents: 182
diff changeset
212 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
213
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
214 if (sub.length() > 0)
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
215 result.push_back(sub);
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
217 a = b + delimeter.length();
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
220 // 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
221 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
222 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
223
1
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 return result;
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 }
4dd5bde4e777 - a whole lot of supplementary source code!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226
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
227 /*!
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
228 * \brief Converts this string to an integer.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
229 * \param ok An pointer to a boolean to store whether or not the conversion was successful.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
230 * If \c ok is \c NULL, the success state is not stored.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
231 * \param base The base to interpret this string with.
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
232 * \returns the resulting integer.
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
233 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
234 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
235 {
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
236 errno = 0;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
237 char* endPointer;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
238 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
239 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
240 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
241 return result;
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 else
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
244 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
245 return {};
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
246 }
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
247 }
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
248
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
249 /*!
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
250 * \brief Removes leading and trailing whitespace from this string. Alternatively a custom filter can be used to strip
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 * something else than whitespace.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
252 * \param filter The filtering function to use.
12c93c4a137c Documented mystring.cpp. String::trim removed as it does the same as String::removeFromEnd.
Teemu Piippo <teemu@compsta2.com>
parents: 145
diff changeset
253 */
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
254 void normalize(std::string& string, int (*filter)(int))
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
255 {
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
256 int a = 0;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
257 int b = string.length() - 1;
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
258 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
259 {
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
260 ++a;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
261 }
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
262 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
263 {
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
264 --b;
182
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
265 }
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
266 if (a == b)
182
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 string = "";
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 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
271 {
20ca0a6be175 removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents: 181
diff changeset
272 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
273 }
107
ca10837a2a9e Backport string changes from protocol5
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
274 }
ca10837a2a9e Backport string changes from protocol5
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
275
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
276 END_ZFC_NAMESPACE

mercurial