token_functions.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // Boost token_functions.hpp ------------------------------------------------//
  2. // Copyright John R. Bandela 2001.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/tokenizer/ for documentation.
  7. // Revision History:
  8. // 01 Oct 2004 Joaquin M Lopez Munoz
  9. // Workaround for a problem with string::assign in msvc-stlport
  10. // 06 Apr 2004 John Bandela
  11. // Fixed a bug involving using char_delimiter with a true input iterator
  12. // 28 Nov 2003 Robert Zeh and John Bandela
  13. // Converted into "fast" functions that avoid using += when
  14. // the supplied iterator isn't an input_iterator; based on
  15. // some work done at Archelon and a version that was checked into
  16. // the boost CVS for a short period of time.
  17. // 20 Feb 2002 John Maddock
  18. // Removed using namespace std declarations and added
  19. // workaround for BOOST_NO_STDC_NAMESPACE (the library
  20. // can be safely mixed with regex).
  21. // 06 Feb 2002 Jeremy Siek
  22. // Added char_separator.
  23. // 02 Feb 2002 Jeremy Siek
  24. // Removed tabs and a little cleanup.
  25. #ifndef BOOST_TOKEN_FUNCTIONS_JRB120303_HPP_
  26. #define BOOST_TOKEN_FUNCTIONS_JRB120303_HPP_
  27. #include <vector>
  28. #include <stdexcept>
  29. #include <string>
  30. #include <cctype>
  31. #include <algorithm> // for find_if
  32. #include <boost/config.hpp>
  33. #include <boost/assert.hpp>
  34. #include <boost/detail/workaround.hpp>
  35. #include <boost/mpl/if.hpp>
  36. #if !defined(BOOST_NO_CWCTYPE)
  37. #include <cwctype>
  38. #endif
  39. //
  40. // the following must not be macros if we are to prefix them
  41. // with std:: (they shouldn't be macros anyway...)
  42. //
  43. #ifdef ispunct
  44. # undef ispunct
  45. #endif
  46. #ifdef iswpunct
  47. # undef iswpunct
  48. #endif
  49. #ifdef isspace
  50. # undef isspace
  51. #endif
  52. #ifdef iswspace
  53. # undef iswspace
  54. #endif
  55. //
  56. // fix namespace problems:
  57. //
  58. #ifdef BOOST_NO_STDC_NAMESPACE
  59. namespace std{
  60. using ::ispunct;
  61. using ::isspace;
  62. #if !defined(BOOST_NO_CWCTYPE)
  63. using ::iswpunct;
  64. using ::iswspace;
  65. #endif
  66. }
  67. #endif
  68. namespace boost{
  69. //===========================================================================
  70. // The escaped_list_separator class. Which is a model of TokenizerFunction
  71. // An escaped list is a super-set of what is commonly known as a comma
  72. // separated value (csv) list.It is separated into fields by a comma or
  73. // other character. If the delimiting character is inside quotes, then it is
  74. // counted as a regular character.To allow for embedded quotes in a field,
  75. // there can be escape sequences using the \ much like C.
  76. // The role of the comma, the quotation mark, and the escape
  77. // character (backslash \), can be assigned to other characters.
  78. struct escaped_list_error : public std::runtime_error{
  79. escaped_list_error(const std::string& what_arg):std::runtime_error(what_arg) { }
  80. };
  81. // The out of the box GCC 2.95 on cygwin does not have a char_traits class.
  82. // MSVC does not like the following typename
  83. template <class Char,
  84. class Traits = BOOST_DEDUCED_TYPENAME std::basic_string<Char>::traits_type >
  85. class escaped_list_separator {
  86. private:
  87. typedef std::basic_string<Char,Traits> string_type;
  88. struct char_eq {
  89. Char e_;
  90. char_eq(Char e):e_(e) { }
  91. bool operator()(Char c) {
  92. return Traits::eq(e_,c);
  93. }
  94. };
  95. string_type escape_;
  96. string_type c_;
  97. string_type quote_;
  98. bool last_;
  99. bool is_escape(Char e) {
  100. char_eq f(e);
  101. return std::find_if(escape_.begin(),escape_.end(),f)!=escape_.end();
  102. }
  103. bool is_c(Char e) {
  104. char_eq f(e);
  105. return std::find_if(c_.begin(),c_.end(),f)!=c_.end();
  106. }
  107. bool is_quote(Char e) {
  108. char_eq f(e);
  109. return std::find_if(quote_.begin(),quote_.end(),f)!=quote_.end();
  110. }
  111. template <typename iterator, typename Token>
  112. void do_escape(iterator& next,iterator end,Token& tok) {
  113. if (++next == end)
  114. throw escaped_list_error(std::string("cannot end with escape"));
  115. if (Traits::eq(*next,'n')) {
  116. tok+='\n';
  117. return;
  118. }
  119. else if (is_quote(*next)) {
  120. tok+=*next;
  121. return;
  122. }
  123. else if (is_c(*next)) {
  124. tok+=*next;
  125. return;
  126. }
  127. else if (is_escape(*next)) {
  128. tok+=*next;
  129. return;
  130. }
  131. else
  132. throw escaped_list_error(std::string("unknown escape sequence"));
  133. }
  134. public:
  135. explicit escaped_list_separator(Char e = '\\',
  136. Char c = ',',Char q = '\"')
  137. : escape_(1,e), c_(1,c), quote_(1,q), last_(false) { }
  138. escaped_list_separator(string_type e, string_type c, string_type q)
  139. : escape_(e), c_(c), quote_(q), last_(false) { }
  140. void reset() {last_=false;}
  141. template <typename InputIterator, typename Token>
  142. bool operator()(InputIterator& next,InputIterator end,Token& tok) {
  143. bool bInQuote = false;
  144. tok = Token();
  145. if (next == end) {
  146. if (last_) {
  147. last_ = false;
  148. return true;
  149. }
  150. else
  151. return false;
  152. }
  153. last_ = false;
  154. for (;next != end;++next) {
  155. if (is_escape(*next)) {
  156. do_escape(next,end,tok);
  157. }
  158. else if (is_c(*next)) {
  159. if (!bInQuote) {
  160. // If we are not in quote, then we are done
  161. ++next;
  162. // The last character was a c, that means there is
  163. // 1 more blank field
  164. last_ = true;
  165. return true;
  166. }
  167. else tok+=*next;
  168. }
  169. else if (is_quote(*next)) {
  170. bInQuote=!bInQuote;
  171. }
  172. else {
  173. tok += *next;
  174. }
  175. }
  176. return true;
  177. }
  178. };
  179. //===========================================================================
  180. // The classes here are used by offset_separator and char_separator to implement
  181. // faster assigning of tokens using assign instead of +=
  182. namespace tokenizer_detail {
  183. //===========================================================================
  184. // Tokenizer was broken for wide character separators, at least on Windows, since
  185. // CRT functions isspace etc only expect values in [0, 0xFF]. Debug build asserts
  186. // if higher values are passed in. The traits extension class should take care of this.
  187. // Assuming that the conditional will always get optimized out in the function
  188. // implementations, argument types are not a problem since both forms of character classifiers
  189. // expect an int.
  190. // In case there is no cwctype header, we implement the checks manually.
  191. // We make use of the fact that the tested categories should fit in ASCII.
  192. template<typename traits>
  193. struct traits_extension : public traits {
  194. typedef typename traits::char_type char_type;
  195. static bool isspace(char_type c)
  196. {
  197. #if !defined(BOOST_NO_CWCTYPE)
  198. if (sizeof(char_type) == 1)
  199. return std::isspace(c) != 0;
  200. else
  201. return std::iswspace(c) != 0;
  202. #else
  203. return static_cast< unsigned >(c) <= 255 && std::isspace(c) != 0;
  204. #endif
  205. }
  206. static bool ispunct(char_type c)
  207. {
  208. #if !defined(BOOST_NO_CWCTYPE)
  209. if (sizeof(char_type) == 1)
  210. return std::ispunct(c) != 0;
  211. else
  212. return std::iswpunct(c) != 0;
  213. #else
  214. return static_cast< unsigned >(c) <= 255 && std::ispunct(c) != 0;
  215. #endif
  216. }
  217. };
  218. // The assign_or_plus_equal struct contains functions that implement
  219. // assign, +=, and clearing based on the iterator type. The
  220. // generic case does nothing for plus_equal and clearing, while
  221. // passing through the call for assign.
  222. //
  223. // When an input iterator is being used, the situation is reversed.
  224. // The assign method does nothing, plus_equal invokes operator +=,
  225. // and the clearing method sets the supplied token to the default
  226. // token constructor's result.
  227. //
  228. template<class IteratorTag>
  229. struct assign_or_plus_equal {
  230. template<class Iterator, class Token>
  231. static void assign(Iterator b, Iterator e, Token &t) {
  232. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) &&\
  233. BOOST_WORKAROUND(__SGI_STL_PORT, < 0x500) &&\
  234. defined(_STLP_DEBUG) &&\
  235. (defined(_STLP_USE_DYNAMIC_LIB) || defined(_DLL))
  236. // Problem with string::assign for msvc-stlport in debug mode: the
  237. // linker tries to import the templatized version of this memfun,
  238. // which is obviously not exported.
  239. // See http://www.stlport.com/dcforum/DCForumID6/1763.html for details.
  240. t = Token();
  241. while(b != e) t += *b++;
  242. #else
  243. t.assign(b, e);
  244. #endif
  245. }
  246. template<class Token, class Value>
  247. static void plus_equal(Token &, const Value &) { }
  248. // If we are doing an assign, there is no need for the
  249. // the clear.
  250. //
  251. template<class Token>
  252. static void clear(Token &) { }
  253. };
  254. template <>
  255. struct assign_or_plus_equal<std::input_iterator_tag> {
  256. template<class Iterator, class Token>
  257. static void assign(Iterator b, Iterator e, Token &t) { }
  258. template<class Token, class Value>
  259. static void plus_equal(Token &t, const Value &v) {
  260. t += v;
  261. }
  262. template<class Token>
  263. static void clear(Token &t) {
  264. t = Token();
  265. }
  266. };
  267. template<class Iterator>
  268. struct pointer_iterator_category{
  269. typedef std::random_access_iterator_tag type;
  270. };
  271. template<class Iterator>
  272. struct class_iterator_category{
  273. typedef typename Iterator::iterator_category type;
  274. };
  275. // This portably gets the iterator_tag without partial template specialization
  276. template<class Iterator>
  277. struct get_iterator_category{
  278. typedef typename mpl::if_<is_pointer<Iterator>,
  279. pointer_iterator_category<Iterator>,
  280. class_iterator_category<Iterator>
  281. >::type cat;
  282. typedef typename cat::type iterator_category;
  283. };
  284. } // namespace tokenizer_detail
  285. //===========================================================================
  286. // The offset_separator class, which is a model of TokenizerFunction.
  287. // Offset breaks a string into tokens based on a range of offsets
  288. class offset_separator {
  289. private:
  290. std::vector<int> offsets_;
  291. unsigned int current_offset_;
  292. bool wrap_offsets_;
  293. bool return_partial_last_;
  294. public:
  295. template <typename Iter>
  296. offset_separator(Iter begin, Iter end, bool wrap_offsets = true,
  297. bool return_partial_last = true)
  298. : offsets_(begin,end), current_offset_(0),
  299. wrap_offsets_(wrap_offsets),
  300. return_partial_last_(return_partial_last) { }
  301. offset_separator()
  302. : offsets_(1,1), current_offset_(),
  303. wrap_offsets_(true), return_partial_last_(true) { }
  304. void reset() {
  305. current_offset_ = 0;
  306. }
  307. template <typename InputIterator, typename Token>
  308. bool operator()(InputIterator& next, InputIterator end, Token& tok)
  309. {
  310. typedef tokenizer_detail::assign_or_plus_equal<
  311. BOOST_DEDUCED_TYPENAME tokenizer_detail::get_iterator_category<
  312. InputIterator
  313. >::iterator_category
  314. > assigner;
  315. BOOST_ASSERT(!offsets_.empty());
  316. assigner::clear(tok);
  317. InputIterator start(next);
  318. if (next == end)
  319. return false;
  320. if (current_offset_ == offsets_.size())
  321. {
  322. if (wrap_offsets_)
  323. current_offset_=0;
  324. else
  325. return false;
  326. }
  327. int c = offsets_[current_offset_];
  328. int i = 0;
  329. for (; i < c; ++i) {
  330. if (next == end)break;
  331. assigner::plus_equal(tok,*next++);
  332. }
  333. assigner::assign(start,next,tok);
  334. if (!return_partial_last_)
  335. if (i < (c-1) )
  336. return false;
  337. ++current_offset_;
  338. return true;
  339. }
  340. };
  341. //===========================================================================
  342. // The char_separator class breaks a sequence of characters into
  343. // tokens based on the character delimiters (very much like bad old
  344. // strtok). A delimiter character can either be kept or dropped. A
  345. // kept delimiter shows up as an output token, whereas a dropped
  346. // delimiter does not.
  347. // This class replaces the char_delimiters_separator class. The
  348. // constructor for the char_delimiters_separator class was too
  349. // confusing and needed to be deprecated. However, because of the
  350. // default arguments to the constructor, adding the new constructor
  351. // would cause ambiguity, so instead I deprecated the whole class.
  352. // The implementation of the class was also simplified considerably.
  353. enum empty_token_policy { drop_empty_tokens, keep_empty_tokens };
  354. // The out of the box GCC 2.95 on cygwin does not have a char_traits class.
  355. template <typename Char,
  356. typename Tr = BOOST_DEDUCED_TYPENAME std::basic_string<Char>::traits_type >
  357. class char_separator
  358. {
  359. typedef tokenizer_detail::traits_extension<Tr> Traits;
  360. typedef std::basic_string<Char,Traits> string_type;
  361. public:
  362. explicit
  363. char_separator(const Char* dropped_delims,
  364. const Char* kept_delims = 0,
  365. empty_token_policy empty_tokens = drop_empty_tokens)
  366. : m_dropped_delims(dropped_delims),
  367. m_use_ispunct(false),
  368. m_use_isspace(false),
  369. m_empty_tokens(empty_tokens),
  370. m_output_done(false)
  371. {
  372. // Borland workaround
  373. if (kept_delims)
  374. m_kept_delims = kept_delims;
  375. }
  376. // use ispunct() for kept delimiters and isspace for dropped.
  377. explicit
  378. char_separator()
  379. : m_use_ispunct(true),
  380. m_use_isspace(true),
  381. m_empty_tokens(drop_empty_tokens) { }
  382. void reset() { }
  383. template <typename InputIterator, typename Token>
  384. bool operator()(InputIterator& next, InputIterator end, Token& tok)
  385. {
  386. typedef tokenizer_detail::assign_or_plus_equal<
  387. BOOST_DEDUCED_TYPENAME tokenizer_detail::get_iterator_category<
  388. InputIterator
  389. >::iterator_category
  390. > assigner;
  391. assigner::clear(tok);
  392. // skip past all dropped_delims
  393. if (m_empty_tokens == drop_empty_tokens)
  394. for (; next != end && is_dropped(*next); ++next)
  395. { }
  396. InputIterator start(next);
  397. if (m_empty_tokens == drop_empty_tokens) {
  398. if (next == end)
  399. return false;
  400. // if we are on a kept_delims move past it and stop
  401. if (is_kept(*next)) {
  402. assigner::plus_equal(tok,*next);
  403. ++next;
  404. } else
  405. // append all the non delim characters
  406. for (; next != end && !is_dropped(*next) && !is_kept(*next); ++next)
  407. assigner::plus_equal(tok,*next);
  408. }
  409. else { // m_empty_tokens == keep_empty_tokens
  410. // Handle empty token at the end
  411. if (next == end)
  412. {
  413. if (m_output_done == false)
  414. {
  415. m_output_done = true;
  416. assigner::assign(start,next,tok);
  417. return true;
  418. }
  419. else
  420. return false;
  421. }
  422. if (is_kept(*next)) {
  423. if (m_output_done == false)
  424. m_output_done = true;
  425. else {
  426. assigner::plus_equal(tok,*next);
  427. ++next;
  428. m_output_done = false;
  429. }
  430. }
  431. else if (m_output_done == false && is_dropped(*next)) {
  432. m_output_done = true;
  433. }
  434. else {
  435. if (is_dropped(*next))
  436. start=++next;
  437. for (; next != end && !is_dropped(*next) && !is_kept(*next); ++next)
  438. assigner::plus_equal(tok,*next);
  439. m_output_done = true;
  440. }
  441. }
  442. assigner::assign(start,next,tok);
  443. return true;
  444. }
  445. private:
  446. string_type m_kept_delims;
  447. string_type m_dropped_delims;
  448. bool m_use_ispunct;
  449. bool m_use_isspace;
  450. empty_token_policy m_empty_tokens;
  451. bool m_output_done;
  452. bool is_kept(Char E) const
  453. {
  454. if (m_kept_delims.length())
  455. return m_kept_delims.find(E) != string_type::npos;
  456. else if (m_use_ispunct) {
  457. return Traits::ispunct(E) != 0;
  458. } else
  459. return false;
  460. }
  461. bool is_dropped(Char E) const
  462. {
  463. if (m_dropped_delims.length())
  464. return m_dropped_delims.find(E) != string_type::npos;
  465. else if (m_use_isspace) {
  466. return Traits::isspace(E) != 0;
  467. } else
  468. return false;
  469. }
  470. };
  471. //===========================================================================
  472. // The following class is DEPRECATED, use class char_separators instead.
  473. //
  474. // The char_delimiters_separator class, which is a model of
  475. // TokenizerFunction. char_delimiters_separator breaks a string
  476. // into tokens based on character delimiters. There are 2 types of
  477. // delimiters. returnable delimiters can be returned as
  478. // tokens. These are often punctuation. nonreturnable delimiters
  479. // cannot be returned as tokens. These are often whitespace
  480. // The out of the box GCC 2.95 on cygwin does not have a char_traits class.
  481. template <class Char,
  482. class Tr = BOOST_DEDUCED_TYPENAME std::basic_string<Char>::traits_type >
  483. class char_delimiters_separator {
  484. private:
  485. typedef tokenizer_detail::traits_extension<Tr> Traits;
  486. typedef std::basic_string<Char,Traits> string_type;
  487. string_type returnable_;
  488. string_type nonreturnable_;
  489. bool return_delims_;
  490. bool no_ispunct_;
  491. bool no_isspace_;
  492. bool is_ret(Char E)const
  493. {
  494. if (returnable_.length())
  495. return returnable_.find(E) != string_type::npos;
  496. else{
  497. if (no_ispunct_) {return false;}
  498. else{
  499. int r = Traits::ispunct(E);
  500. return r != 0;
  501. }
  502. }
  503. }
  504. bool is_nonret(Char E)const
  505. {
  506. if (nonreturnable_.length())
  507. return nonreturnable_.find(E) != string_type::npos;
  508. else{
  509. if (no_isspace_) {return false;}
  510. else{
  511. int r = Traits::isspace(E);
  512. return r != 0;
  513. }
  514. }
  515. }
  516. public:
  517. explicit char_delimiters_separator(bool return_delims = false,
  518. const Char* returnable = 0,
  519. const Char* nonreturnable = 0)
  520. : returnable_(returnable ? returnable : string_type().c_str()),
  521. nonreturnable_(nonreturnable ? nonreturnable:string_type().c_str()),
  522. return_delims_(return_delims), no_ispunct_(returnable!=0),
  523. no_isspace_(nonreturnable!=0) { }
  524. void reset() { }
  525. public:
  526. template <typename InputIterator, typename Token>
  527. bool operator()(InputIterator& next, InputIterator end,Token& tok) {
  528. tok = Token();
  529. // skip past all nonreturnable delims
  530. // skip past the returnable only if we are not returning delims
  531. for (;next!=end && ( is_nonret(*next) || (is_ret(*next)
  532. && !return_delims_ ) );++next) { }
  533. if (next == end) {
  534. return false;
  535. }
  536. // if we are to return delims and we are one a returnable one
  537. // move past it and stop
  538. if (is_ret(*next) && return_delims_) {
  539. tok+=*next;
  540. ++next;
  541. }
  542. else
  543. // append all the non delim characters
  544. for (;next!=end && !is_nonret(*next) && !is_ret(*next);++next)
  545. tok+=*next;
  546. return true;
  547. }
  548. };
  549. } //namespace boost
  550. #endif