src/misc.cpp

changeset 286
7a562bf3d829
parent 268
778eed342ee4
child 308
4e2425bd4dc7
equal deleted inserted replaced
285:836e77323ab0 286:7a562bf3d829
135 str ftoa (double num) { 135 str ftoa (double num) {
136 // Disable the locale first so that the decimal point will not 136 // Disable the locale first so that the decimal point will not
137 // turn into anything weird (like commas) 137 // turn into anything weird (like commas)
138 setlocale (LC_NUMERIC, "C"); 138 setlocale (LC_NUMERIC, "C");
139 139
140 str rep = fmt ("%f", num); 140 str rep;
141 rep.sprintf ("%f", num);
141 142
142 // Remove trailing zeroes 143 // Remove trailing zeroes
143 while (rep[~rep - 1] == '0') 144 while (rep.right (1) == "0")
144 rep -= 1; 145 rep.chop (1);
145 146
146 // If there was only zeroes in the decimal place, remove 147 // If there were only zeroes in the decimal place, remove
147 // the decimal point now. 148 // the decimal point now.
148 if (rep[~rep - 1] == '.') 149 if (rep.right (1) == ".")
149 rep -= 1; 150 rep.chop (1);
150 151
151 return rep; 152 return rep;
152 } 153 }
153 154
154 // ============================================================================= 155 // =============================================================================
155 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 156 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
156 // ============================================================================= 157 // =============================================================================
157 bool isNumber (const str& tok) { 158 bool isNumber (const str& tok) {
158 bool gotDot = false; 159 bool gotDot = false;
159 160
160 for (const char& c : tok) { 161 for (int i = 0; i < tok.length (); ++i) {
162 const qchar c = tok[i];
163
161 // Allow leading hyphen for negatives 164 // Allow leading hyphen for negatives
162 if (&c == &tok[0] && c == '-') 165 if (i == 0 && c == '-')
163 continue; 166 continue;
164 167
165 // Check for decimal point 168 // Check for decimal point
166 if (!gotDot && c == '.') { 169 if (!gotDot && c == '.') {
167 gotDot = true; 170 gotDot = true;
236 edit_rotpoint_x = pos[X]; 239 edit_rotpoint_x = pos[X];
237 edit_rotpoint_y = pos[Y]; 240 edit_rotpoint_y = pos[Y];
238 edit_rotpoint_z = pos[Z]; 241 edit_rotpoint_z = pos[Z];
239 } 242 }
240 243
244 str join (initlist<StringFormatArg> vals, str delim) {
245 QStringList list;
246 for (const StringFormatArg& arg : vals)
247 list << arg.value ();
248
249 return list.join (delim);
250 }
251
252
241 // ============================================================================= 253 // =============================================================================
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
243 // ============================================================================= 255 // =============================================================================
244 StringParser::StringParser (str inText, char sep) { 256 StringParser::StringParser (str inText, char sep) {
245 m_tokens = inText.split (sep); 257 m_tokens = container_cast<QStringList, vector<str>> (inText.split (sep, QString::SkipEmptyParts));
246 m_pos = -1; 258 m_pos = -1;
247 } 259 }
248 260
249 // ----------------------------------------------------------------------------- 261 // -----------------------------------------------------------------------------
250 bool StringParser::atBeginning () { 262 bool StringParser::atBeginning () {

mercurial