util.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "util.h"
  2. #include <util/generic/utility.h>
  3. #include <cstdio>
  4. #include <cstdarg>
  5. #include <cstdlib>
  6. int a2i(const TString& s) {
  7. return atoi(s.c_str());
  8. }
  9. //============================== span =====================================
  10. void str_spn::init(const char* charset, bool extended) {
  11. // chars_table_1 is necessary to avoid some unexpected
  12. // multi-threading issues
  13. ui8 chars_table_1[256];
  14. memset(chars_table_1, 0, sizeof(chars_table_1));
  15. if (extended) {
  16. for (const char* cs = charset; *cs; cs++) {
  17. if (cs[1] == '-' && cs[2] != 0) {
  18. for (int c = (ui8)*cs; c <= (ui8)cs[2]; c++) {
  19. chars_table_1[c] = 1;
  20. }
  21. cs += 2;
  22. continue;
  23. }
  24. chars_table_1[(ui8)*cs] = 1;
  25. }
  26. } else {
  27. for (; *charset; charset++) {
  28. chars_table_1[(ui8)*charset] = 1;
  29. }
  30. }
  31. memcpy(chars_table, chars_table_1, 256);
  32. chars_table_1[0] = 1;
  33. for (int n = 0; n < 256; n++) {
  34. c_chars_table[n] = !chars_table_1[n];
  35. }
  36. }
  37. Tr::Tr(const char* from, const char* to) {
  38. for (size_t n = 0; n < 256; n++) {
  39. Map[n] = (char)n;
  40. }
  41. for (; *from && *to; from++, to++) {
  42. Map[(ui8)*from] = *to;
  43. }
  44. }
  45. size_t Tr::FindFirstChangePosition(const TString& str) const {
  46. for (auto it = str.begin(); it != str.end(); ++it) {
  47. if (ConvertChar(*it) != *it) {
  48. return it - str.begin();
  49. }
  50. }
  51. return TString::npos;
  52. }
  53. void Tr::Do(TString& str) const {
  54. const size_t changePosition = FindFirstChangePosition(str);
  55. if (changePosition == TString::npos) {
  56. return;
  57. }
  58. for (auto it = str.begin() + changePosition; it != str.end(); ++it) {
  59. *it = ConvertChar(*it);
  60. }
  61. }