| 1 | /*************************************************************************** |
|---|
| 2 | * toolsString.cc |
|---|
| 3 | * |
|---|
| 4 | * Sun Oct 30 2007 |
|---|
| 5 | * Copyright 2007 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #include <string.h> |
|---|
| 33 | #include <ctype.h> |
|---|
| 34 | #include <stdarg.h> |
|---|
| 35 | #include <stdlib.h> |
|---|
| 36 | #include <stdio.h> |
|---|
| 37 | |
|---|
| 38 | #include <libdodo/toolsString.h> |
|---|
| 39 | #include <libdodo/types.h> |
|---|
| 40 | |
|---|
| 41 | using namespace dodo::tools; |
|---|
| 42 | |
|---|
| 43 | bool |
|---|
| 44 | string::equal(const dodo::string &first, |
|---|
| 45 | const dodo::string &second) |
|---|
| 46 | { |
|---|
| 47 | unsigned long firstSize = first.size(); |
|---|
| 48 | |
|---|
| 49 | if (firstSize != second.size()) |
|---|
| 50 | return false; |
|---|
| 51 | |
|---|
| 52 | if (firstSize == 0) |
|---|
| 53 | return true; |
|---|
| 54 | |
|---|
| 55 | for (unsigned long i = 0; i < firstSize; ++i) |
|---|
| 56 | if (first[i] != second[i]) |
|---|
| 57 | return false; |
|---|
| 58 | |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | //------------------------------------------------------------------- |
|---|
| 63 | |
|---|
| 64 | bool |
|---|
| 65 | string::iequal(const dodo::string &first, |
|---|
| 66 | const dodo::string &second) |
|---|
| 67 | { |
|---|
| 68 | unsigned long firstSize = first.size(); |
|---|
| 69 | |
|---|
| 70 | if (firstSize != second.size()) |
|---|
| 71 | return false; |
|---|
| 72 | |
|---|
| 73 | if (firstSize == 0) |
|---|
| 74 | return true; |
|---|
| 75 | |
|---|
| 76 | for (unsigned long i = 0; i < firstSize; ++i) |
|---|
| 77 | if (tolower(first[i]) != tolower(second[i])) |
|---|
| 78 | return false; |
|---|
| 79 | |
|---|
| 80 | return true; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | //------------------------------------------------------------------- |
|---|
| 84 | |
|---|
| 85 | bool |
|---|
| 86 | string::contains(const dodo::string &str, |
|---|
| 87 | const dodo::string &needle, |
|---|
| 88 | bool icase) |
|---|
| 89 | { |
|---|
| 90 | if (icase) { |
|---|
| 91 | if (strcasestr(str.data(), needle.data()) != NULL) |
|---|
| 92 | return true; |
|---|
| 93 | } else if (strstr(str.data(), needle.data()) != NULL) |
|---|
| 94 | return true; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | return false; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | //------------------------------------------------------------------- |
|---|
| 101 | |
|---|
| 102 | unsigned long |
|---|
| 103 | string::find(const dodo::string &str, |
|---|
| 104 | const dodo::string &needle, |
|---|
| 105 | unsigned long position, |
|---|
| 106 | bool icase) |
|---|
| 107 | { |
|---|
| 108 | if (position > str.size()) |
|---|
| 109 | return dodo::string::POSITION_END; |
|---|
| 110 | |
|---|
| 111 | char *pos = NULL; |
|---|
| 112 | |
|---|
| 113 | if (icase) { |
|---|
| 114 | if ((pos = strcasestr((char *)(str.data() + position), needle.data())) == NULL) |
|---|
| 115 | return dodo::string::POSITION_END; |
|---|
| 116 | else |
|---|
| 117 | return (pos - str.data()); |
|---|
| 118 | } else { |
|---|
| 119 | if ((pos = strstr((char *)(str.data() + position), needle.data())) == NULL) |
|---|
| 120 | return dodo::string::POSITION_END; |
|---|
| 121 | else |
|---|
| 122 | return (pos - str.data()); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return dodo::string::POSITION_END; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | //------------------------------------------------------------------- |
|---|
| 129 | |
|---|
| 130 | unsigned long |
|---|
| 131 | string::find(const dodo::string &str, |
|---|
| 132 | const dodo::string &needle, |
|---|
| 133 | bool icase) |
|---|
| 134 | { |
|---|
| 135 | return find(str, needle, 0, icase); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | //------------------------------------------------------------------- |
|---|
| 139 | |
|---|
| 140 | dodo::string |
|---|
| 141 | string::format(const dodo::string &format, |
|---|
| 142 | ...) |
|---|
| 143 | { |
|---|
| 144 | unsigned long length = format.size() * 3; |
|---|
| 145 | char *str = new char[length + 1]; |
|---|
| 146 | |
|---|
| 147 | va_list ap; |
|---|
| 148 | |
|---|
| 149 | va_start(ap, format); |
|---|
| 150 | vsnprintf(str, length, format.data(), ap); |
|---|
| 151 | va_end(ap); |
|---|
| 152 | |
|---|
| 153 | dodo::string res = str; |
|---|
| 154 | |
|---|
| 155 | delete [] str; |
|---|
| 156 | |
|---|
| 157 | return res; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | //------------------------------------------------------------------- |
|---|
| 161 | |
|---|
| 162 | dodo::string |
|---|
| 163 | string::lToString(long number) |
|---|
| 164 | { |
|---|
| 165 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 166 | sprintf(temp, "%ld", number); |
|---|
| 167 | |
|---|
| 168 | return temp; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | //------------------------------------------------------------------- |
|---|
| 172 | |
|---|
| 173 | dodo::string |
|---|
| 174 | string::ulToString(unsigned long number) |
|---|
| 175 | { |
|---|
| 176 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 177 | sprintf(temp, "%ld", number); |
|---|
| 178 | |
|---|
| 179 | return temp; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | //------------------------------------------------------------------- |
|---|
| 183 | |
|---|
| 184 | dodo::string |
|---|
| 185 | string::iToString(int number) |
|---|
| 186 | { |
|---|
| 187 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 188 | sprintf(temp, "%d", number); |
|---|
| 189 | |
|---|
| 190 | return temp; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | //------------------------------------------------------------------- |
|---|
| 194 | |
|---|
| 195 | dodo::string |
|---|
| 196 | string::uiToString(unsigned int number) |
|---|
| 197 | { |
|---|
| 198 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 199 | sprintf(temp, "%u", number); |
|---|
| 200 | |
|---|
| 201 | return temp; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | //------------------------------------------------------------------- |
|---|
| 205 | |
|---|
| 206 | dodo::string |
|---|
| 207 | string::sToString(short number) |
|---|
| 208 | { |
|---|
| 209 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 210 | sprintf(temp, "%hd", number); |
|---|
| 211 | |
|---|
| 212 | return temp; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | //------------------------------------------------------------------- |
|---|
| 216 | |
|---|
| 217 | dodo::string |
|---|
| 218 | string::usToString(unsigned short number) |
|---|
| 219 | { |
|---|
| 220 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 221 | sprintf(temp, "%hd", number); |
|---|
| 222 | |
|---|
| 223 | return temp; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | //------------------------------------------------------------------- |
|---|
| 227 | |
|---|
| 228 | dodo::string |
|---|
| 229 | string::fToString(float number) |
|---|
| 230 | { |
|---|
| 231 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 232 | sprintf(temp, "%f", number); |
|---|
| 233 | |
|---|
| 234 | return temp; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | //------------------------------------------------------------------- |
|---|
| 238 | |
|---|
| 239 | dodo::string |
|---|
| 240 | string::dToString(double number) |
|---|
| 241 | { |
|---|
| 242 | char temp[NUMERIC_STRING_SIZE]; |
|---|
| 243 | sprintf(temp, "%f", number); |
|---|
| 244 | |
|---|
| 245 | return temp; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | //------------------------------------------------------------------- |
|---|
| 249 | |
|---|
| 250 | dodo::string |
|---|
| 251 | string::lTrim(const dodo::string &data, |
|---|
| 252 | char symbol) |
|---|
| 253 | { |
|---|
| 254 | int size = data.size(), i(0); |
|---|
| 255 | |
|---|
| 256 | for (; i < size; ++i) |
|---|
| 257 | if (data[i] != symbol) |
|---|
| 258 | break; |
|---|
| 259 | |
|---|
| 260 | return dodo::string(data.data() + i, size - i); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | //------------------------------------------------------------------- |
|---|
| 264 | |
|---|
| 265 | dodo::string |
|---|
| 266 | string::rTrim(const dodo::string &data, |
|---|
| 267 | char symbol) |
|---|
| 268 | { |
|---|
| 269 | int i(data.size() - 1); |
|---|
| 270 | |
|---|
| 271 | for (; i >= 0; --i) |
|---|
| 272 | if (data[i] != symbol) |
|---|
| 273 | break; |
|---|
| 274 | |
|---|
| 275 | return dodo::string(data.data(), i + 1); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | //------------------------------------------------------------------- |
|---|
| 279 | |
|---|
| 280 | dodo::string |
|---|
| 281 | string::rTrim(const dodo::string &data, |
|---|
| 282 | const char symbols[], |
|---|
| 283 | int symCount) |
|---|
| 284 | { |
|---|
| 285 | int i(data.size() - 1), j, empty; |
|---|
| 286 | |
|---|
| 287 | for (; i >= 0; --i) { |
|---|
| 288 | for (j = 0, empty = 0; j < symCount; ++j) |
|---|
| 289 | if (data[i] != symbols[j]) |
|---|
| 290 | ++empty; |
|---|
| 291 | if (empty == symCount) |
|---|
| 292 | break; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | return dodo::string(data.data(), i + 1); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | //------------------------------------------------------------------- |
|---|
| 299 | |
|---|
| 300 | dodo::string |
|---|
| 301 | string::lTrim(const dodo::string &data, |
|---|
| 302 | const char symbols[], |
|---|
| 303 | int symCount) |
|---|
| 304 | { |
|---|
| 305 | int size = data.size(), i(0), empty, j; |
|---|
| 306 | |
|---|
| 307 | for (; i < size; ++i) { |
|---|
| 308 | for (j = 0, empty = 0; j < symCount; ++j) |
|---|
| 309 | if (data[i] != symbols[j]) |
|---|
| 310 | ++empty; |
|---|
| 311 | if (empty == symCount) |
|---|
| 312 | break; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | return dodo::string(data.data() + i, size - i); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | //------------------------------------------------------------------- |
|---|
| 319 | |
|---|
| 320 | dodo::string |
|---|
| 321 | string::trim(const dodo::string &data, |
|---|
| 322 | const char symbols[], |
|---|
| 323 | int symCount) |
|---|
| 324 | { |
|---|
| 325 | return rTrim(lTrim(data, symbols, symCount), symbols, symCount); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | //------------------------------------------------------------------- |
|---|
| 329 | |
|---|
| 330 | dodo::string |
|---|
| 331 | string::trim(const dodo::string &data, |
|---|
| 332 | char symbol) |
|---|
| 333 | { |
|---|
| 334 | return rTrim(lTrim(data, symbol), symbol); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | //------------------------------------------------------------------- |
|---|
| 338 | |
|---|
| 339 | long |
|---|
| 340 | string::stringToL(const dodo::string &data) |
|---|
| 341 | { |
|---|
| 342 | return atol(data.data()); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | //------------------------------------------------------------------- |
|---|
| 346 | |
|---|
| 347 | unsigned long |
|---|
| 348 | string::stringToUL(const dodo::string &data) |
|---|
| 349 | { |
|---|
| 350 | return strtoul(data.data(), NULL, 10); |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | //------------------------------------------------------------------- |
|---|
| 354 | |
|---|
| 355 | int |
|---|
| 356 | string::stringToI(const dodo::string &data) |
|---|
| 357 | { |
|---|
| 358 | return atoi(data.data()); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | //------------------------------------------------------------------- |
|---|
| 362 | |
|---|
| 363 | unsigned int |
|---|
| 364 | string::stringToUI(const dodo::string &data) |
|---|
| 365 | { |
|---|
| 366 | return (unsigned int)atol(data.data()); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | //------------------------------------------------------------------- |
|---|
| 370 | |
|---|
| 371 | short |
|---|
| 372 | string::stringToS(const dodo::string &data) |
|---|
| 373 | { |
|---|
| 374 | return (short)atoi(data.data()); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | //------------------------------------------------------------------- |
|---|
| 378 | |
|---|
| 379 | unsigned short |
|---|
| 380 | string::stringToUS(const dodo::string &data) |
|---|
| 381 | { |
|---|
| 382 | return (unsigned short)atoi(data.data()); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | //------------------------------------------------------------------- |
|---|
| 386 | |
|---|
| 387 | float |
|---|
| 388 | string::stringToF(const dodo::string &data) |
|---|
| 389 | { |
|---|
| 390 | return (float)atof(data.data()); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | //------------------------------------------------------------------- |
|---|
| 394 | |
|---|
| 395 | double |
|---|
| 396 | string::stringToD(const dodo::string &data) |
|---|
| 397 | { |
|---|
| 398 | return atof(data.data()); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | //------------------------------------------------------------------- |
|---|
| 402 | |
|---|
| 403 | void |
|---|
| 404 | string::replace(const dodoStringArray &needle, |
|---|
| 405 | const dodoStringArray &replacement, |
|---|
| 406 | dodo::string &data) |
|---|
| 407 | { |
|---|
| 408 | dodoStringArray::const_iterator i(needle.begin()), j(needle.end()); |
|---|
| 409 | dodoStringArray::const_iterator o(replacement.begin()), p(replacement.end()); |
|---|
| 410 | for (; i != j && o != p; ++i, ++o) |
|---|
| 411 | replace(*i, *o, data); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | //------------------------------------------------------------------- |
|---|
| 415 | |
|---|
| 416 | void |
|---|
| 417 | string::replace(const dodo::string &needle, |
|---|
| 418 | const dodo::string &replacement, |
|---|
| 419 | dodo::string &data) |
|---|
| 420 | { |
|---|
| 421 | unsigned long i(0), j(needle.size()), k(replacement.size()); |
|---|
| 422 | |
|---|
| 423 | while (true) { |
|---|
| 424 | i = data.find(needle, i); |
|---|
| 425 | if (i == dodo::string::POSITION_END) |
|---|
| 426 | break; |
|---|
| 427 | |
|---|
| 428 | data.replace(i, j, replacement); |
|---|
| 429 | i += k; |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | //------------------------------------------------------------------- |
|---|
| 434 | |
|---|