util.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2021 Duowan Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __X_PACK_UTIL_H
  17. #define __X_PACK_UTIL_H
  18. #include <string>
  19. #include <vector>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "traits.h"
  23. #include "numeric.h"
  24. namespace xpack {
  25. struct cmp_str {
  26. bool operator()(char const *a, char const *b) const {
  27. return strcmp(a, b) < 0;
  28. }
  29. };
  30. class Util {
  31. public:
  32. // if n<0 will split all
  33. static size_t split(std::vector<std::string>&slice, const std::string&str, char c, int n = -1) {
  34. size_t last = 0;
  35. size_t pos = 0;
  36. int cnt = 0;
  37. while (std::string::npos != (pos=str.find(c, last))) {
  38. slice.push_back(str.substr(last, pos-last));
  39. last = pos+1;
  40. ++cnt;
  41. if (n>0 && n>=cnt) {
  42. break;
  43. }
  44. }
  45. slice.push_back(str.substr(last));
  46. return slice.size();
  47. }
  48. static size_t split(std::vector<std::string>&slice, const std::string&str, const std::string& sp, int n =-1) {
  49. size_t last = 0;
  50. size_t pos = 0;
  51. size_t len = sp.length();
  52. if (len == 0) {
  53. slice.push_back(str);
  54. return 1;
  55. }
  56. int cnt = 0;
  57. while (std::string::npos != (pos=str.find(sp, last))) {
  58. slice.push_back(str.substr(last, pos-last));
  59. last = pos+len;
  60. ++cnt;
  61. if (n>0 && n>=cnt) {
  62. break;
  63. }
  64. }
  65. slice.push_back(str.substr(last));
  66. return slice.size();
  67. }
  68. // not support float.
  69. template <class T>
  70. static typename x_enable_if<numeric<T>::is_integer, std::string>::type itoa(const T&val) {
  71. char buf[128];
  72. size_t i = sizeof(buf)-1;
  73. if (val == 0) {
  74. return std::string("0");
  75. }
  76. T _tmp = val>0?val:-val;
  77. while (_tmp > 0) {
  78. buf[i--] = char(int('0')+int(_tmp%10));
  79. _tmp /= 10;
  80. }
  81. if (val < 0) {
  82. buf[i--] = '-';
  83. }
  84. return std::string(&buf[i+1], sizeof(buf)-i-1);
  85. }
  86. // not support float. and only decimal
  87. template <class T>
  88. static typename x_enable_if<numeric<T>::is_integer, bool>::type atoi(const std::string&s, T&val) {
  89. if (s.empty()) {
  90. return false;
  91. }
  92. T _tmp = 0;
  93. size_t i = 0;
  94. if (s[0] == '-') {
  95. if (s.length() == 1) {
  96. return false;
  97. } else if (s.length()>2 && s[1]=='0') {
  98. return false;
  99. }
  100. for (i=1; i<s.length(); ++i) {
  101. if (s[i]>='0' && s[i]<='9') {
  102. T _c = _tmp*10 - (s[i]-'0');
  103. if (_c < _tmp) {
  104. _tmp = _c;
  105. } else if (i > 0) {
  106. return false; // overflow
  107. }
  108. } else {
  109. return false;
  110. }
  111. }
  112. } else {
  113. if (s[0] == '+') {
  114. ++i;
  115. }
  116. if (s[i]=='0' && i+1<s.length()) {
  117. return false;
  118. }
  119. for (; i<s.length(); ++i) {
  120. if (s[i]>='0' && s[i]<='9') {
  121. T _c = _tmp*10 + (s[i]-'0');
  122. if (_c > _tmp) {
  123. _tmp = _c;
  124. } else if (i > 0) {
  125. return false; // overflow
  126. }
  127. } else {
  128. return false;
  129. }
  130. }
  131. }
  132. val = _tmp;
  133. return true;
  134. }
  135. template <class T>
  136. static typename x_enable_if<numeric<T>::is_integer, bool>::type atoi(const char*s, T&val) {
  137. if (NULL == s) {
  138. return false;
  139. }
  140. std::string _t(s);
  141. return atoi(_t, val);
  142. }
  143. };
  144. }
  145. #endif