split_iterator.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "split_iterator.h"
  2. #include <util/system/yassert.h>
  3. #include <cctype>
  4. #include <cstring>
  5. #include <cstdlib>
  6. /****************** TSplitDelimiters2 ******************/
  7. TSplitDelimiters::TSplitDelimiters(const char* s) {
  8. memset(Delims, 0, sizeof(Delims));
  9. while (*s)
  10. Delims[(ui8) * (s++)] = true;
  11. }
  12. /****************** TSplitBase ******************/
  13. TSplitBase::TSplitBase(const char* str, size_t length)
  14. : Str(str)
  15. , Len(length)
  16. {
  17. }
  18. TSplitBase::TSplitBase(const TString& s)
  19. : Str(s.data())
  20. , Len(s.size())
  21. {
  22. }
  23. /****************** TDelimitersSplit ******************/
  24. TDelimitersSplit::TDelimitersSplit(const char* str, size_t length, const TSplitDelimiters& delimiters)
  25. : TSplitBase(str, length)
  26. , Delimiters(delimiters)
  27. {
  28. }
  29. TDelimitersSplit::TDelimitersSplit(const TString& s, const TSplitDelimiters& delimiters)
  30. : TSplitBase(s)
  31. , Delimiters(delimiters)
  32. {
  33. }
  34. size_t TDelimitersSplit::Begin() const {
  35. size_t pos = 0;
  36. while ((pos < Len) && Delimiters.IsDelimiter(Str[pos]))
  37. ++pos;
  38. return pos;
  39. }
  40. TSizeTRegion TDelimitersSplit::Next(size_t& pos) const {
  41. size_t begin = pos;
  42. while ((pos < Len) && !Delimiters.IsDelimiter(Str[pos]))
  43. ++pos;
  44. TSizeTRegion result(begin, pos);
  45. while ((pos < Len) && Delimiters.IsDelimiter(Str[pos]))
  46. ++pos;
  47. return result;
  48. }
  49. TDelimitersSplit::TIterator TDelimitersSplit::Iterator() const {
  50. return TIterator(*this);
  51. }
  52. /****************** TDelimitersStrictSplit ******************/
  53. TDelimitersStrictSplit::TDelimitersStrictSplit(const char* str, size_t length, const TSplitDelimiters& delimiters)
  54. : TSplitBase(str, length)
  55. , Delimiters(delimiters)
  56. {
  57. }
  58. TDelimitersStrictSplit::TDelimitersStrictSplit(const TString& s, const TSplitDelimiters& delimiters)
  59. : TSplitBase(s)
  60. , Delimiters(delimiters)
  61. {
  62. }
  63. TDelimitersStrictSplit::TIterator TDelimitersStrictSplit::Iterator() const {
  64. return TIterator(*this);
  65. }
  66. TSizeTRegion TDelimitersStrictSplit::Next(size_t& pos) const {
  67. size_t begin = pos;
  68. while ((pos < Len) && !Delimiters.IsDelimiter(Str[pos]))
  69. ++pos;
  70. TSizeTRegion result(begin, pos);
  71. if (pos < Len)
  72. ++pos;
  73. return result;
  74. }
  75. size_t TDelimitersStrictSplit::Begin() const {
  76. return 0;
  77. }
  78. /****************** TScreenedDelimitersSplit ******************/
  79. TScreenedDelimitersSplit::TScreenedDelimitersSplit(const TString& s, const TSplitDelimiters& delimiters, const TSplitDelimiters& screens)
  80. : TSplitBase(s)
  81. , Delimiters(delimiters)
  82. , Screens(screens)
  83. {
  84. }
  85. TScreenedDelimitersSplit::TScreenedDelimitersSplit(const char* str, size_t length, const TSplitDelimiters& delimiters, const TSplitDelimiters& screens)
  86. : TSplitBase(str, length)
  87. , Delimiters(delimiters)
  88. , Screens(screens)
  89. {
  90. }
  91. TScreenedDelimitersSplit::TIterator TScreenedDelimitersSplit::Iterator() const {
  92. return TIterator(*this);
  93. }
  94. TSizeTRegion TScreenedDelimitersSplit::Next(size_t& pos) const {
  95. size_t begin = pos;
  96. bool screened = false;
  97. while (pos < Len) {
  98. if (Screens.IsDelimiter(Str[pos]))
  99. screened = !screened;
  100. if (Delimiters.IsDelimiter(Str[pos]) && !screened)
  101. break;
  102. ++pos;
  103. }
  104. TSizeTRegion result(begin, pos);
  105. if (pos < Len)
  106. ++pos;
  107. return result;
  108. }
  109. size_t TScreenedDelimitersSplit::Begin() const {
  110. return 0;
  111. }
  112. /****************** TDelimitersSplitWithoutTags ******************/
  113. TDelimitersSplitWithoutTags::TDelimitersSplitWithoutTags(const char* str, size_t length, const TSplitDelimiters& delimiters)
  114. : TSplitBase(str, length)
  115. , Delimiters(delimiters)
  116. {
  117. }
  118. TDelimitersSplitWithoutTags::TDelimitersSplitWithoutTags(const TString& s, const TSplitDelimiters& delimiters)
  119. : TSplitBase(s)
  120. , Delimiters(delimiters)
  121. {
  122. }
  123. size_t TDelimitersSplitWithoutTags::SkipTag(size_t pos) const {
  124. Y_ASSERT('<' == Str[pos]);
  125. while ((pos < Len) && ('>' != Str[pos]))
  126. ++pos;
  127. return pos + 1;
  128. }
  129. size_t TDelimitersSplitWithoutTags::SkipDelimiters(size_t pos) const {
  130. while (true) {
  131. while ((pos < Len) && Delimiters.IsDelimiter(Str[pos]) && ('<' != Str[pos]))
  132. ++pos;
  133. if (pos < Len) {
  134. if ('<' != Str[pos])
  135. break;
  136. else
  137. pos = SkipTag(pos);
  138. } else
  139. break;
  140. }
  141. return pos;
  142. }
  143. size_t TDelimitersSplitWithoutTags::Begin() const {
  144. size_t pos = 0;
  145. pos = SkipDelimiters(pos);
  146. return pos;
  147. }
  148. TSizeTRegion TDelimitersSplitWithoutTags::Next(size_t& pos) const {
  149. size_t begin = pos;
  150. while ((pos < Len) && !Delimiters.IsDelimiter(Str[pos]) && ('<' != Str[pos]))
  151. ++pos;
  152. TSizeTRegion result(begin, pos);
  153. pos = SkipDelimiters(pos);
  154. return result;
  155. }
  156. TDelimitersSplitWithoutTags::TIterator TDelimitersSplitWithoutTags::Iterator() const {
  157. return TIterator(*this);
  158. }
  159. /****************** TCharSplit ******************/
  160. TCharSplit::TCharSplit(const char* str, size_t length)
  161. : TSplitBase(str, length)
  162. {
  163. }
  164. TCharSplit::TCharSplit(const TString& s)
  165. : TSplitBase(s)
  166. {
  167. }
  168. TCharSplit::TIterator TCharSplit::Iterator() const {
  169. return TIterator(*this);
  170. }
  171. TSizeTRegion TCharSplit::Next(size_t& pos) const {
  172. TSizeTRegion result(pos, pos + 1);
  173. ++pos;
  174. return result;
  175. }
  176. size_t TCharSplit::Begin() const {
  177. return 0;
  178. }
  179. /****************** TCharSplitWithoutTags ******************/
  180. TCharSplitWithoutTags::TCharSplitWithoutTags(const char* str, size_t length)
  181. : TSplitBase(str, length)
  182. {
  183. }
  184. TCharSplitWithoutTags::TCharSplitWithoutTags(const TString& s)
  185. : TSplitBase(s)
  186. {
  187. }
  188. size_t TCharSplitWithoutTags::SkipTag(size_t pos) const {
  189. Y_ASSERT('<' == Str[pos]);
  190. while ((pos < Len) && ('>' != Str[pos]))
  191. ++pos;
  192. return pos + 1;
  193. }
  194. size_t TCharSplitWithoutTags::SkipDelimiters(size_t pos) const {
  195. while (true) {
  196. if (pos < Len) {
  197. if ('<' != Str[pos])
  198. break;
  199. else
  200. pos = SkipTag(pos);
  201. } else
  202. break;
  203. }
  204. return pos;
  205. }
  206. size_t TCharSplitWithoutTags::Begin() const {
  207. size_t pos = 0;
  208. pos = SkipDelimiters(pos);
  209. return pos;
  210. }
  211. TSizeTRegion TCharSplitWithoutTags::Next(size_t& pos) const {
  212. size_t begin = pos++;
  213. TSizeTRegion result(begin, pos);
  214. pos = SkipDelimiters(pos);
  215. return result;
  216. }
  217. TCharSplitWithoutTags::TIterator TCharSplitWithoutTags::Iterator() const {
  218. return TIterator(*this);
  219. }
  220. TSubstringSplitDelimiter::TSubstringSplitDelimiter(const TString& s)
  221. : Matcher(s)
  222. , Len(s.size())
  223. {
  224. }
  225. /****************** TSubstringSplit ******************/
  226. TSubstringSplit::TSubstringSplit(const char* str, size_t length, const TSubstringSplitDelimiter& delimiter)
  227. : TSplitBase(str, length)
  228. , Delimiter(delimiter)
  229. {
  230. }
  231. TSubstringSplit::TSubstringSplit(const TString& str, const TSubstringSplitDelimiter& delimiter)
  232. : TSplitBase(str)
  233. , Delimiter(delimiter)
  234. {
  235. }
  236. TSubstringSplit::TIterator TSubstringSplit::Iterator() const {
  237. return TIterator(*this);
  238. }
  239. TSizeTRegion TSubstringSplit::Next(size_t& pos) const {
  240. const char* begin = Str + pos;
  241. const char* end = Str + Len;
  242. const char* delim;
  243. if (Delimiter.Matcher.SubStr(begin, end, delim)) {
  244. TSizeTRegion result(pos, delim - begin + pos);
  245. pos += delim - begin + Delimiter.Len;
  246. return result;
  247. } else {
  248. TSizeTRegion result(pos, end - begin + pos);
  249. pos += end - begin;
  250. return result;
  251. }
  252. }
  253. size_t TSubstringSplit::Begin() const {
  254. return 0;
  255. }