You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/tokeniser_functions.hpp
  3. *
  4. * Purpose: Restartable tokenising functions.
  5. *
  6. * Created: 6th January 2001
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2001-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/string/tokeniser_functions.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::find_next_token suite of
  42. * restartable tokenising functions
  43. * (\ref group__library__string "String" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS
  46. #define STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS_REVISION 2
  51. # define STLSOFT_VER_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS_EDIT 22
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. #ifdef STLSOFT_UNITTEST
  60. # if !defined(STLSOFT_COMPILER_IS_WATCOM)
  61. # if defined(STLSOFT_COMPILER_IS_MWERKS) && \
  62. ((__MWERKS__ & 0xff00) < 0x3000)
  63. # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
  64. # include <stlsoft/string/simple_string.hpp>
  65. # endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
  66. # else /* ? compiler */
  67. # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW
  68. # include <stlsoft/string/string_view.hpp>
  69. # endif /* STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW */
  70. # endif /* compiler */
  71. # include <vector>
  72. # endif /* compiler */
  73. #endif /* STLSOFT_UNITTEST */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Namespace
  76. */
  77. #ifndef _STLSOFT_NO_NAMESPACE
  78. namespace stlsoft
  79. {
  80. #endif /* _STLSOFT_NO_NAMESPACE */
  81. /* /////////////////////////////////////////////////////////////////////////
  82. * Functions
  83. */
  84. /** \brief Adjusts the delimiters into a token string to find the next
  85. * token, according to the given delimiter. Processing stops when \c p1
  86. * is equal to \c end
  87. *
  88. * \ingroup group__library__string
  89. *
  90. * \param p0 Pointer to the current token in the string. Will be set to the
  91. * next when the function returns
  92. * \param p1 Pointer to the current token in the string. Will be set to the
  93. * end of the token when the function returns
  94. * \param end The end of the string
  95. * \param delim The delimiter
  96. *
  97. * \return p0
  98. *
  99. * \pre NULL != p0
  100. * \pre NULL != p1
  101. * \pre NULL != end
  102. * \pre p0 <= p1
  103. * \pre p1 <= end
  104. */
  105. template <ss_typename_param_k C>
  106. inline C const*
  107. find_next_token(
  108. C const*& p0
  109. , C const*& p1
  110. #if defined(STLSOFT_COMPILER_IS_DMC)
  111. , C const* end
  112. #else /* ? compiler */
  113. , C const* const end
  114. #endif /* compiler */
  115. , C delim
  116. )
  117. {
  118. STLSOFT_ASSERT(NULL != p0);
  119. STLSOFT_ASSERT(NULL != p1);
  120. STLSOFT_ASSERT(NULL != end);
  121. STLSOFT_ASSERT(p0 <= p1);
  122. STLSOFT_ASSERT(p1 <= end);
  123. if(p1 != end)
  124. {
  125. if(p0 != p1)
  126. {
  127. p0 = ++p1;
  128. }
  129. else
  130. {
  131. p0 = p1;
  132. }
  133. if(delim == *p1)
  134. {
  135. ++p0;
  136. ++p1;
  137. }
  138. else for(; p1 != end; ++p1)
  139. {
  140. if(delim == *p1)
  141. {
  142. break;
  143. }
  144. }
  145. }
  146. else
  147. {
  148. p0 = end;
  149. }
  150. return p0;
  151. }
  152. /** \brief Adjusts the delimiters into a token string to find the next
  153. * token, according to the given delimiter. Processing stops when \c p1
  154. * points to the nul-terminating character
  155. *
  156. * \ingroup group__library__string
  157. *
  158. * \param p0 Pointer to the current token in the string. Will be set to the
  159. * next when the function returns
  160. * \param p1 Pointer to the current token in the string. Will be set to the
  161. * end of the token when the function returns
  162. * \param delim The delimiter
  163. *
  164. * \return p0
  165. *
  166. * \pre NULL != p0
  167. * \pre NULL != p1
  168. * \pre p0 <= p1
  169. */
  170. template <ss_typename_param_k C>
  171. inline bool
  172. find_next_token(
  173. C const*& p0
  174. , C const*& p1
  175. , C delim
  176. )
  177. {
  178. STLSOFT_ASSERT(NULL != p0);
  179. STLSOFT_ASSERT(NULL != p1);
  180. STLSOFT_ASSERT(p0 <= p1);
  181. if('\0' == *p1)
  182. {
  183. p0 = p1;
  184. return false;
  185. }
  186. if(p0 != p1)
  187. {
  188. p0 = ++p1;
  189. }
  190. else
  191. {
  192. p0 = p1;
  193. }
  194. if(delim == *p1)
  195. {
  196. ++p0;
  197. ++p1;
  198. }
  199. else for(;; ++p1)
  200. {
  201. if('\0' == *p1)
  202. {
  203. return p0 != p1;
  204. }
  205. else if(delim == *p1)
  206. {
  207. break;
  208. }
  209. }
  210. return true;
  211. }
  212. /* /////////////////////////////////////////////////////////////////////////
  213. * Unit-testing
  214. */
  215. #ifdef STLSOFT_UNITTEST
  216. # include "./unittest/tokeniser_functions_unittest_.h"
  217. #endif /* STLSOFT_UNITTEST */
  218. /* ////////////////////////////////////////////////////////////////////// */
  219. #ifndef _STLSOFT_NO_NAMESPACE
  220. } // namespace stlsoft
  221. #endif /* _STLSOFT_NO_NAMESPACE */
  222. /* ////////////////////////////////////////////////////////////////////// */
  223. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_TOKENISER_FUNCTIONS */
  224. /* ///////////////////////////// end of file //////////////////////////// */