Splits a gcode command by each word/argument, doesn't care about spaces. This command is about the same speed as the string.split(" ") command, but might be a little faster using precompiled regex.
| 260 | //* but might be a little faster using precompiled regex. |
| 261 | //*/ |
| 262 | QStringList GcodePreprocessorUtils::splitCommand(const QString &command) { |
| 263 | QStringList l; |
| 264 | bool readNumeric = false; |
| 265 | QString sb; |
| 266 | |
| 267 | QByteArray ba(command.toLatin1()); |
| 268 | const char *cmd = ba.constData(); // Direct access to string data |
| 269 | char c; |
| 270 | |
| 271 | for (int i = 0; i < command.length(); i++) { |
| 272 | c = cmd[i]; |
| 273 | |
| 274 | if (readNumeric && !isDigit(c) && c != '.') { |
| 275 | readNumeric = false; |
| 276 | l.append(sb); |
| 277 | sb.clear(); |
| 278 | if (isLetter(c)) sb.append(c); |
| 279 | } else if (isDigit(c) || c == '.' || c == '-') { |
| 280 | sb.append(c); |
| 281 | readNumeric = true; |
| 282 | } else if (isLetter(c)) sb.append(c); |
| 283 | } |
| 284 | |
| 285 | if (sb.length() > 0) l.append(sb); |
| 286 | |
| 287 | // QChar c; |
| 288 | |
| 289 | // for (int i = 0; i < command.length(); i++) { |
| 290 | // c = command[i]; |
| 291 | |
| 292 | // if (readNumeric && !c.isDigit() && c != '.') { |
| 293 | // readNumeric = false; |
| 294 | // l.append(sb); |
| 295 | // sb = ""; |
| 296 | // if (c.isLetter()) sb.append(c); |
| 297 | // } else if (c.isDigit() || c == '.' || c == '-') { |
| 298 | // sb.append(c); |
| 299 | // readNumeric = true; |
| 300 | // } else if (c.isLetter()) sb.append(c); |
| 301 | // } |
| 302 | |
| 303 | // if (sb.length() > 0) l.append(sb); |
| 304 | |
| 305 | return l; |
| 306 | } |
| 307 | |
| 308 | // TODO: Replace everything that uses this with a loop that loops through |
| 309 | // the string and creates a hash with all the values. |