src/str.cc

Sun, 19 Jan 2014 20:16:00 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 19 Jan 2014 20:16:00 +0200
changeset 82
841562f5a32f
parent 79
2425fa6a4f21
child 85
264a61e9eba0
permissions
-rw-r--r--

- added the public-domain updaterevision so I can have access to git stuff
- lexer #include now works properly! woot!
- merged commands.def and events.def to botc_defs.bts. This is essentially the "zcommon.acs" of botc.

75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 Copyright (c) 2013-2014, Santeri Piippo
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 All rights reserved.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions are met:
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * Redistributions of source code must retain the above copyright
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 notice, this list of conditions and the following disclaimer.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * Redistributions in binary form must reproduce the above copyright
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * Neither the name of the <organization> nor the
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 names of its contributors may be used to endorse or promote products
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 derived from this software without specific prior written permission.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 */
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include <cstring>
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include "main.h"
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "str.h"
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 int string::compare (const string& other) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 return m_string.compare (other.std_string());
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 void string::trim (string::length_type n)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 if (n > 0)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 m_string = substring (0, length() - n).std_string();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 else
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 m_string = substring (n, -1).std_string();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 string string::strip (list<char> unwanted)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 string copy (m_string);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 for (char c : unwanted)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 for (int i = 0; i < copy.length(); ++i)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 if (copy[i] == c)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 copy.erase (i);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 /*
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 while(( pos = copy.first( c )) != -1 )
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 copy.erase( pos );
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 */
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 return copy;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 string string::to_uppercase() const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 string newstr = m_string;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 for (char& c : newstr)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 if (c >= 'a' && c <= 'z')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 c -= 'a' - 'A';
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 return newstr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 string string::to_lowercase() const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 string newstr = m_string;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 for (char & c : newstr)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 if (c >= 'A' && c <= 'Z')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 c += 'a' - 'A';
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 return newstr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 string_list string::split (char del) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 string delimstr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 delimstr += del;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 return split (delimstr);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 string_list string::split (string del) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 string_list res;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 long a = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 // Find all separators and store the text left to them.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 for (;;)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 long b = first (del, a);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 if (b == -1)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 break;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 string sub = substring (a, b);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 if (sub.length() > 0)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 res.push_back (substring (a, b));
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 a = b + strlen (del);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 // Add the string at the right of the last separator
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 if (a < (int) length())
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 res.push_back (substring (a, length()));
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 return res;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 void string::replace (const char* a, const char* b)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 long pos;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 while ( (pos = first (a)) != -1)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 m_string = m_string.replace (pos, strlen (a), b);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 int string::count (const char needle) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 int needles = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 for (const char & c : m_string)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 if (c == needle)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 needles++;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 return needles;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 string string::substring (long a, long b) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 if (b == -1)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 b = length();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 if (b == a)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 return "";
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 if (b < a)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 // Swap the variables
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 int c = a;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 a = b;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 b = c;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 char* newstr = new char[b - a + 1];
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 strncpy (newstr, m_string.c_str() + a, b - a);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 newstr[b - a] = '\0';
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 string other (newstr);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 delete[] newstr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 return other;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 string::length_type string::posof (int n) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 int count = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 for (int i = 0; i < length(); ++i)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 if (m_string[i] != ' ')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 continue;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 if (++count < n)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 continue;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 return i;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 return -1;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 int string::first (const char* c, string::length_type a) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 for (; a < length(); a++)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 if (m_string[a] == c[0] && strncmp (m_string.c_str() + a, c, strlen (c)) == 0)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 return a;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 return -1;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 int string::last (const char* c, string::length_type a) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 if (a == -1 || a >= length())
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 a = length() - 1;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 for (; a > 0; a--)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 if (m_string[a] == c[0] && strncmp (m_string.c_str() + a, c, strlen (c)) == 0)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 return a;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 return -1;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 void string::dump() const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 print ("`%1`:\n", chars());
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 int i = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 for (char u : m_string)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 print ("\t%1. [%d2] `%3`\n", i++, u, string (u));
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 long string::to_long (bool* ok, int base) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 errno = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 char* endptr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 long i = strtol (m_string.c_str(), &endptr, base);
79
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
249
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
250 if (ok)
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
251 *ok = (errno == 0 && *endptr == '\0');
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
252
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 return i;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 float string::to_float (bool* ok) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260 errno = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 char* endptr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 float i = strtof (m_string.c_str(), &endptr);
79
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
263
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
264 if (ok)
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
265 *ok = (errno == 0 && *endptr == '\0');
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
266
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267 return i;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 double string::to_double (bool* ok) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
274 errno = 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 char* endptr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 double i = strtod (m_string.c_str(), &endptr);
79
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
277
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
278 if (ok)
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
279 *ok = (errno == 0 && *endptr == '\0');
2425fa6a4f21 - parser now processes commands.def properly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 75
diff changeset
280
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281 return i;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
286 bool operator== (const char* a, const string& b)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
287 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
288 return b == a;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
289 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
290
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
291 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
292 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
293 string operator+ (const char* a, const string& b)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
294 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
295 return string (a) + b;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
296 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
297
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
298 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
299 //
82
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
300 string string::operator+ (const string& data) const
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
301 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
302 string newString = *this;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
303 newString += data;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
304 return newString;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
305 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
306
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
307 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
308 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
309 string string::operator+ (const char* data) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
310 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
311 string newstr = *this;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
312 newstr += data;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
313 return newstr;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
314 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
315
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
316 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
317 //
82
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
318 string string::operator+ (int num) const
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
319 {
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
320 string newstr = *this;
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
321 string numstr;
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
322 numstr.sprintf ("%d", num);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
323 newstr += numstr;
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
324 return newstr;
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
325 }
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
326
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
327 // =============================================================================
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
328 //
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
329 string& string::operator+= (const string data)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
330 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
331 append (data);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
332 return *this;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
333 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
334
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
335 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
336 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
337 string& string::operator+= (const char* data)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
338 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
339 append (data);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
340 return *this;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
341 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
342
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
343 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
344 //
82
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
345 string& string::operator+= (int num)
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
346 {
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
347 string numstr;
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
348 numstr.sprintf ("%d", num);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
349 return operator+= (numstr);
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
350 }
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
351
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
352 // =============================================================================
841562f5a32f - added the public-domain updaterevision so I can have access to git stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 79
diff changeset
353 //
75
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
354 bool string::is_numeric() const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
355 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
356 bool gotDot = false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
357
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
358 for (const char & c : m_string)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
359 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
360 // Allow leading hyphen for negatives
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
361 if (&c == &m_string[0] && c == '-')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
362 continue;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
363
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
364 // Check for decimal point
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
365 if (!gotDot && c == '.')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
366 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
367 gotDot = true;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
368 continue;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
369 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
370
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
371 if (c >= '0' && c <= '9')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
372 continue; // Digit
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
373
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
374 // If the above cases didn't catch this character, it was
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
375 // illegal and this is therefore not a number.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
376 return false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
377 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
378
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
379 return true;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
380 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
381
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
382 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
383 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
384 bool string::ends_with (const string& other)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
385 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
386 if (length() < other.length())
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
387 return false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
388
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
389 const int ofs = length() - other.length();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
390 return strncmp (chars() + ofs, other.chars(), other.length()) == 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
391 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
392
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
393 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
394 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
395 bool string::starts_with (const string& other)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
396 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
397 if (length() < other.length())
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
398 return false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
399
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
400 return strncmp (chars(), other.chars(), other.length()) == 0;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
401 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
402
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
403 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
404 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
405 void string::sprintf (const char* fmtstr, ...)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
406 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
407 char* buf;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
408 int bufsize = 256;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
409 va_list va;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
410 va_start (va, fmtstr);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
411
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
412 do
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
413 buf = new char[bufsize];
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
414
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
415 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
416
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
417 va_end (va);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
418 m_string = buf;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
419 delete[] buf;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
420 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
421
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
422 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
423 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
424 void string::prepend (string a)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
425 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
426 m_string = (a + m_string).std_string();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
427 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
428
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
429 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
430 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
431 string string_list::join (const string& delim)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
432 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
433 string result;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
434
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
435 for (const string & it : std_deque())
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
436 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
437 if (!result.is_empty())
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
438 result += delim;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
439
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
440 result += it;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
441 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
442
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
443 return result;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
444 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
445
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
446 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
447 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
448 bool string::mask (const string& pattern) const
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
449 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
450 // Elevate to uppercase for case-insensitive matching
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
451 string pattern_upper = pattern.to_uppercase();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
452 string this_upper = to_uppercase();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
453 const char* maskstring = pattern_upper.chars();
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
454 const char* mptr = &maskstring[0];
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
455
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
456 for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
457 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
458 if (*mptr == '?')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
459 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
460 if (* (sptr + 1) == '\0')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
461 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
462 // ? demands that there's a character here and there wasn't.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
463 // Therefore, mask matching fails
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
464 return false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
465 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
466 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
467
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
468 elif (*mptr == '*')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
469 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
470 char end = * (++mptr);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
471
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
472 // If '*' is the final character of the message, all of the remaining
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
473 // string matches against the '*'. We don't need to bother checking
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
474 // the string any further.
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
475 if (end == '\0')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
476 return true;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
477
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
478 // Skip to the end character
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
479 while (*sptr != end && *sptr != '\0')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
480 sptr++;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
481
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
482 // String ended while the mask still had stuff
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
483 if (*sptr == '\0')
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
484 return false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
485 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
486 elif (*sptr != *mptr)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
487 return false;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
488
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
489 mptr++;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
490 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
491
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
492 return true;
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
493 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
494
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
495 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
496 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
497 string string::from_number (int a)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
498 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
499 char buf[32];
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
500 ::sprintf (buf, "%d", a);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
501 return string (buf);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
502 }
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
503
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
504 // =============================================================================
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
505 //
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
506 string string::from_number (long a)
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
507 {
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
508 char buf[32];
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
509 ::sprintf (buf, "%ld", a);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
510 return string (buf);
bf8c57437231 - renamed files to .cxx, restructured parser.cc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
511 }

mercurial