read_until.hpp 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126
  1. //
  2. // read_until.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_READ_UNTIL_HPP
  11. #define BOOST_ASIO_READ_UNTIL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <string>
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/buffer.hpp>
  20. #include <boost/asio/detail/regex_fwd.hpp>
  21. #include <boost/asio/detail/string_view.hpp>
  22. #include <boost/asio/detail/type_traits.hpp>
  23. #include <boost/asio/error.hpp>
  24. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  25. # include <boost/asio/basic_streambuf_fwd.hpp>
  26. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  27. #include <boost/asio/detail/push_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. char (&has_result_type_helper(...))[2];
  32. template <typename T>
  33. char has_result_type_helper(T*, typename T::result_type* = 0);
  34. template <typename T>
  35. struct has_result_type
  36. {
  37. enum { value = (sizeof((has_result_type_helper)((T*)(0))) == 1) };
  38. };
  39. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  40. template <typename> class initiate_async_read_until_delim_v1;
  41. template <typename> class initiate_async_read_until_delim_string_v1;
  42. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  43. template <typename> class initiate_async_read_until_expr_v1;
  44. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  45. template <typename> class initiate_async_read_until_match_v1;
  46. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  47. template <typename> class initiate_async_read_until_delim_v2;
  48. template <typename> class initiate_async_read_until_delim_string_v2;
  49. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  50. template <typename> class initiate_async_read_until_expr_v2;
  51. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  52. template <typename> class initiate_async_read_until_match_v2;
  53. } // namespace detail
  54. /// Type trait used to determine whether a type can be used as a match condition
  55. /// function with read_until and async_read_until.
  56. template <typename T>
  57. struct is_match_condition
  58. {
  59. #if defined(GENERATING_DOCUMENTATION)
  60. /// The value member is true if the type may be used as a match condition.
  61. static const bool value;
  62. #else
  63. enum
  64. {
  65. value = boost::asio::is_function<remove_pointer_t<T>>::value
  66. || detail::has_result_type<T>::value
  67. };
  68. #endif
  69. };
  70. /**
  71. * @defgroup read_until boost::asio::read_until
  72. *
  73. * @brief The @c read_until function is a composed operation that reads data
  74. * into a dynamic buffer sequence, or into a streambuf, until it contains a
  75. * delimiter, matches a regular expression, or a function object indicates a
  76. * match.
  77. */
  78. /*@{*/
  79. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  80. /// Read data into a dynamic buffer sequence until it contains a specified
  81. /// delimiter.
  82. /**
  83. * This function is used to read data into the specified dynamic buffer
  84. * sequence until the dynamic buffer sequence's get area contains the specified
  85. * delimiter. The call will block until one of the following conditions is
  86. * true:
  87. *
  88. * @li The get area of the dynamic buffer sequence contains the specified
  89. * delimiter.
  90. *
  91. * @li An error occurred.
  92. *
  93. * This operation is implemented in terms of zero or more calls to the stream's
  94. * read_some function. If the dynamic buffer sequence's get area already
  95. * contains the delimiter, the function returns immediately.
  96. *
  97. * @param s The stream from which the data is to be read. The type must support
  98. * the SyncReadStream concept.
  99. *
  100. * @param buffers The dynamic buffer sequence into which the data will be read.
  101. *
  102. * @param delim The delimiter character.
  103. *
  104. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  105. * and including the delimiter.
  106. *
  107. * @throws boost::system::system_error Thrown on failure.
  108. *
  109. * @note After a successful read_until operation, the dynamic buffer sequence
  110. * may contain additional data beyond the delimiter. An application will
  111. * typically leave that data in the dynamic buffer sequence for a subsequent
  112. * read_until operation to examine.
  113. *
  114. * @par Example
  115. * To read data into a @c std::string until a newline is encountered:
  116. * @code std::string data;
  117. * std::size_t n = boost::asio::read_until(s,
  118. * boost::asio::dynamic_buffer(data), '\n');
  119. * std::string line = data.substr(0, n);
  120. * data.erase(0, n); @endcode
  121. * After the @c read_until operation completes successfully, the string @c data
  122. * contains the delimiter:
  123. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  124. * The call to @c substr then extracts the data up to and including the
  125. * delimiter, so that the string @c line contains:
  126. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  127. * After the call to @c erase, the remaining data is left in the buffer @c b as
  128. * follows:
  129. * @code { 'd', 'e', ... } @endcode
  130. * This data may be the start of a new line, to be extracted by a subsequent
  131. * @c read_until operation.
  132. */
  133. template <typename SyncReadStream, typename DynamicBuffer_v1>
  134. std::size_t read_until(SyncReadStream& s,
  135. DynamicBuffer_v1&& buffers, char delim,
  136. constraint_t<
  137. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  138. > = 0,
  139. constraint_t<
  140. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  141. > = 0);
  142. /// Read data into a dynamic buffer sequence until it contains a specified
  143. /// delimiter.
  144. /**
  145. * This function is used to read data into the specified dynamic buffer
  146. * sequence until the dynamic buffer sequence's get area contains the specified
  147. * delimiter. The call will block until one of the following conditions is
  148. * true:
  149. *
  150. * @li The get area of the dynamic buffer sequence contains the specified
  151. * delimiter.
  152. *
  153. * @li An error occurred.
  154. *
  155. * This operation is implemented in terms of zero or more calls to the stream's
  156. * read_some function. If the dynamic buffer sequence's get area already
  157. * contains the delimiter, the function returns immediately.
  158. *
  159. * @param s The stream from which the data is to be read. The type must support
  160. * the SyncReadStream concept.
  161. *
  162. * @param buffers The dynamic buffer sequence into which the data will be read.
  163. *
  164. * @param delim The delimiter character.
  165. *
  166. * @param ec Set to indicate what error occurred, if any.
  167. *
  168. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  169. * and including the delimiter. Returns 0 if an error occurred.
  170. *
  171. * @note After a successful read_until operation, the dynamic buffer sequence
  172. * may contain additional data beyond the delimiter. An application will
  173. * typically leave that data in the dynamic buffer sequence for a subsequent
  174. * read_until operation to examine.
  175. */
  176. template <typename SyncReadStream, typename DynamicBuffer_v1>
  177. std::size_t read_until(SyncReadStream& s,
  178. DynamicBuffer_v1&& buffers,
  179. char delim, boost::system::error_code& ec,
  180. constraint_t<
  181. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  182. > = 0,
  183. constraint_t<
  184. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  185. > = 0);
  186. /// Read data into a dynamic buffer sequence until it contains a specified
  187. /// delimiter.
  188. /**
  189. * This function is used to read data into the specified dynamic buffer
  190. * sequence until the dynamic buffer sequence's get area contains the specified
  191. * delimiter. The call will block until one of the following conditions is
  192. * true:
  193. *
  194. * @li The get area of the dynamic buffer sequence contains the specified
  195. * delimiter.
  196. *
  197. * @li An error occurred.
  198. *
  199. * This operation is implemented in terms of zero or more calls to the stream's
  200. * read_some function. If the dynamic buffer sequence's get area already
  201. * contains the delimiter, the function returns immediately.
  202. *
  203. * @param s The stream from which the data is to be read. The type must support
  204. * the SyncReadStream concept.
  205. *
  206. * @param buffers The dynamic buffer sequence into which the data will be read.
  207. *
  208. * @param delim The delimiter string.
  209. *
  210. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  211. * and including the delimiter.
  212. *
  213. * @note After a successful read_until operation, the dynamic buffer sequence
  214. * may contain additional data beyond the delimiter. An application will
  215. * typically leave that data in the dynamic buffer sequence for a subsequent
  216. * read_until operation to examine.
  217. *
  218. * @par Example
  219. * To read data into a @c std::string until a CR-LF sequence is encountered:
  220. * @code std::string data;
  221. * std::size_t n = boost::asio::read_until(s,
  222. * boost::asio::dynamic_buffer(data), "\r\n");
  223. * std::string line = data.substr(0, n);
  224. * data.erase(0, n); @endcode
  225. * After the @c read_until operation completes successfully, the string @c data
  226. * contains the delimiter:
  227. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  228. * The call to @c substr then extracts the data up to and including the
  229. * delimiter, so that the string @c line contains:
  230. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  231. * After the call to @c erase, the remaining data is left in the buffer @c b as
  232. * follows:
  233. * @code { 'd', 'e', ... } @endcode
  234. * This data may be the start of a new line, to be extracted by a subsequent
  235. * @c read_until operation.
  236. */
  237. template <typename SyncReadStream, typename DynamicBuffer_v1>
  238. std::size_t read_until(SyncReadStream& s,
  239. DynamicBuffer_v1&& buffers,
  240. BOOST_ASIO_STRING_VIEW_PARAM delim,
  241. constraint_t<
  242. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  243. > = 0,
  244. constraint_t<
  245. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  246. > = 0);
  247. /// Read data into a dynamic buffer sequence until it contains a specified
  248. /// delimiter.
  249. /**
  250. * This function is used to read data into the specified dynamic buffer
  251. * sequence until the dynamic buffer sequence's get area contains the specified
  252. * delimiter. The call will block until one of the following conditions is
  253. * true:
  254. *
  255. * @li The get area of the dynamic buffer sequence contains the specified
  256. * delimiter.
  257. *
  258. * @li An error occurred.
  259. *
  260. * This operation is implemented in terms of zero or more calls to the stream's
  261. * read_some function. If the dynamic buffer sequence's get area already
  262. * contains the delimiter, the function returns immediately.
  263. *
  264. * @param s The stream from which the data is to be read. The type must support
  265. * the SyncReadStream concept.
  266. *
  267. * @param buffers The dynamic buffer sequence into which the data will be read.
  268. *
  269. * @param delim The delimiter string.
  270. *
  271. * @param ec Set to indicate what error occurred, if any.
  272. *
  273. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  274. * and including the delimiter. Returns 0 if an error occurred.
  275. *
  276. * @note After a successful read_until operation, the dynamic buffer sequence
  277. * may contain additional data beyond the delimiter. An application will
  278. * typically leave that data in the dynamic buffer sequence for a subsequent
  279. * read_until operation to examine.
  280. */
  281. template <typename SyncReadStream, typename DynamicBuffer_v1>
  282. std::size_t read_until(SyncReadStream& s,
  283. DynamicBuffer_v1&& buffers,
  284. BOOST_ASIO_STRING_VIEW_PARAM delim,
  285. boost::system::error_code& ec,
  286. constraint_t<
  287. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  288. > = 0,
  289. constraint_t<
  290. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  291. > = 0);
  292. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  293. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  294. || defined(GENERATING_DOCUMENTATION)
  295. /// Read data into a dynamic buffer sequence until some part of the data it
  296. /// contains matches a regular expression.
  297. /**
  298. * This function is used to read data into the specified dynamic buffer
  299. * sequence until the dynamic buffer sequence's get area contains some data
  300. * that matches a regular expression. The call will block until one of the
  301. * following conditions is true:
  302. *
  303. * @li A substring of the dynamic buffer sequence's get area matches the
  304. * regular expression.
  305. *
  306. * @li An error occurred.
  307. *
  308. * This operation is implemented in terms of zero or more calls to the stream's
  309. * read_some function. If the dynamic buffer sequence's get area already
  310. * contains data that matches the regular expression, the function returns
  311. * immediately.
  312. *
  313. * @param s The stream from which the data is to be read. The type must support
  314. * the SyncReadStream concept.
  315. *
  316. * @param buffers A dynamic buffer sequence into which the data will be read.
  317. *
  318. * @param expr The regular expression.
  319. *
  320. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  321. * and including the substring that matches the regular expression.
  322. *
  323. * @throws boost::system::system_error Thrown on failure.
  324. *
  325. * @note After a successful read_until operation, the dynamic buffer sequence
  326. * may contain additional data beyond that which matched the regular
  327. * expression. An application will typically leave that data in the dynamic
  328. * buffer sequence for a subsequent read_until operation to examine.
  329. *
  330. * @par Example
  331. * To read data into a @c std::string until a CR-LF sequence is encountered:
  332. * @code std::string data;
  333. * std::size_t n = boost::asio::read_until(s,
  334. * boost::asio::dynamic_buffer(data), boost::regex("\r\n"));
  335. * std::string line = data.substr(0, n);
  336. * data.erase(0, n); @endcode
  337. * After the @c read_until operation completes successfully, the string @c data
  338. * contains the delimiter:
  339. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  340. * The call to @c substr then extracts the data up to and including the
  341. * delimiter, so that the string @c line contains:
  342. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  343. * After the call to @c erase, the remaining data is left in the buffer @c b as
  344. * follows:
  345. * @code { 'd', 'e', ... } @endcode
  346. * This data may be the start of a new line, to be extracted by a subsequent
  347. * @c read_until operation.
  348. */
  349. template <typename SyncReadStream, typename DynamicBuffer_v1, typename Traits>
  350. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v1&& buffers,
  351. const boost::basic_regex<char, Traits>& expr,
  352. constraint_t<
  353. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  354. > = 0,
  355. constraint_t<
  356. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  357. > = 0);
  358. /// Read data into a dynamic buffer sequence until some part of the data it
  359. /// contains matches a regular expression.
  360. /**
  361. * This function is used to read data into the specified dynamic buffer
  362. * sequence until the dynamic buffer sequence's get area contains some data
  363. * that matches a regular expression. The call will block until one of the
  364. * following conditions is true:
  365. *
  366. * @li A substring of the dynamic buffer sequence's get area matches the
  367. * regular expression.
  368. *
  369. * @li An error occurred.
  370. *
  371. * This operation is implemented in terms of zero or more calls to the stream's
  372. * read_some function. If the dynamic buffer sequence's get area already
  373. * contains data that matches the regular expression, the function returns
  374. * immediately.
  375. *
  376. * @param s The stream from which the data is to be read. The type must support
  377. * the SyncReadStream concept.
  378. *
  379. * @param buffers A dynamic buffer sequence into which the data will be read.
  380. *
  381. * @param expr The regular expression.
  382. *
  383. * @param ec Set to indicate what error occurred, if any.
  384. *
  385. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  386. * and including the substring that matches the regular expression. Returns 0
  387. * if an error occurred.
  388. *
  389. * @note After a successful read_until operation, the dynamic buffer sequence
  390. * may contain additional data beyond that which matched the regular
  391. * expression. An application will typically leave that data in the dynamic
  392. * buffer sequence for a subsequent read_until operation to examine.
  393. */
  394. template <typename SyncReadStream, typename DynamicBuffer_v1, typename Traits>
  395. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v1&& buffers,
  396. const boost::basic_regex<char, Traits>& expr, boost::system::error_code& ec,
  397. constraint_t<
  398. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  399. > = 0,
  400. constraint_t<
  401. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  402. > = 0);
  403. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  404. // || defined(GENERATING_DOCUMENTATION)
  405. /// Read data into a dynamic buffer sequence until a function object indicates a
  406. /// match.
  407. /**
  408. * This function is used to read data into the specified dynamic buffer
  409. * sequence until a user-defined match condition function object, when applied
  410. * to the data contained in the dynamic buffer sequence, indicates a successful
  411. * match. The call will block until one of the following conditions is true:
  412. *
  413. * @li The match condition function object returns a std::pair where the second
  414. * element evaluates to true.
  415. *
  416. * @li An error occurred.
  417. *
  418. * This operation is implemented in terms of zero or more calls to the stream's
  419. * read_some function. If the match condition function object already indicates
  420. * a match, the function returns immediately.
  421. *
  422. * @param s The stream from which the data is to be read. The type must support
  423. * the SyncReadStream concept.
  424. *
  425. * @param buffers A dynamic buffer sequence into which the data will be read.
  426. *
  427. * @param match_condition The function object to be called to determine whether
  428. * a match exists. The signature of the function object must be:
  429. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  430. * @endcode
  431. * where @c iterator represents the type:
  432. * @code buffers_iterator<typename DynamicBuffer_v1::const_buffers_type>
  433. * @endcode
  434. * The iterator parameters @c begin and @c end define the range of bytes to be
  435. * scanned to determine whether there is a match. The @c first member of the
  436. * return value is an iterator marking one-past-the-end of the bytes that have
  437. * been consumed by the match function. This iterator is used to calculate the
  438. * @c begin parameter for any subsequent invocation of the match condition. The
  439. * @c second member of the return value is true if a match has been found, false
  440. * otherwise.
  441. *
  442. * @returns The number of bytes in the dynamic_buffer's get area that
  443. * have been fully consumed by the match function.
  444. *
  445. * @throws boost::system::system_error Thrown on failure.
  446. *
  447. * @note After a successful read_until operation, the dynamic buffer sequence
  448. * may contain additional data beyond that which matched the function object.
  449. * An application will typically leave that data in the dynamic buffer sequence
  450. * for a subsequent read_until operation to examine.
  451. * @note The default implementation of the @c is_match_condition type trait
  452. * evaluates to true for function pointers and function objects with a
  453. * @c result_type typedef. It must be specialised for other user-defined
  454. * function objects.
  455. *
  456. * @par Examples
  457. * To read data into a dynamic buffer sequence until whitespace is encountered:
  458. * @code typedef boost::asio::buffers_iterator<
  459. * boost::asio::const_buffers_1> iterator;
  460. *
  461. * std::pair<iterator, bool>
  462. * match_whitespace(iterator begin, iterator end)
  463. * {
  464. * iterator i = begin;
  465. * while (i != end)
  466. * if (std::isspace(*i++))
  467. * return std::make_pair(i, true);
  468. * return std::make_pair(i, false);
  469. * }
  470. * ...
  471. * std::string data;
  472. * boost::asio::read_until(s, data, match_whitespace);
  473. * @endcode
  474. *
  475. * To read data into a @c std::string until a matching character is found:
  476. * @code class match_char
  477. * {
  478. * public:
  479. * explicit match_char(char c) : c_(c) {}
  480. *
  481. * template <typename Iterator>
  482. * std::pair<Iterator, bool> operator()(
  483. * Iterator begin, Iterator end) const
  484. * {
  485. * Iterator i = begin;
  486. * while (i != end)
  487. * if (c_ == *i++)
  488. * return std::make_pair(i, true);
  489. * return std::make_pair(i, false);
  490. * }
  491. *
  492. * private:
  493. * char c_;
  494. * };
  495. *
  496. * namespace asio {
  497. * template <> struct is_match_condition<match_char>
  498. * : public boost::true_type {};
  499. * } // namespace asio
  500. * ...
  501. * std::string data;
  502. * boost::asio::read_until(s, data, match_char('a'));
  503. * @endcode
  504. */
  505. template <typename SyncReadStream,
  506. typename DynamicBuffer_v1, typename MatchCondition>
  507. std::size_t read_until(SyncReadStream& s,
  508. DynamicBuffer_v1&& buffers,
  509. MatchCondition match_condition,
  510. constraint_t<
  511. is_match_condition<MatchCondition>::value
  512. > = 0,
  513. constraint_t<
  514. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  515. > = 0,
  516. constraint_t<
  517. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  518. > = 0);
  519. /// Read data into a dynamic buffer sequence until a function object indicates a
  520. /// match.
  521. /**
  522. * This function is used to read data into the specified dynamic buffer
  523. * sequence until a user-defined match condition function object, when applied
  524. * to the data contained in the dynamic buffer sequence, indicates a successful
  525. * match. The call will block until one of the following conditions is true:
  526. *
  527. * @li The match condition function object returns a std::pair where the second
  528. * element evaluates to true.
  529. *
  530. * @li An error occurred.
  531. *
  532. * This operation is implemented in terms of zero or more calls to the stream's
  533. * read_some function. If the match condition function object already indicates
  534. * a match, the function returns immediately.
  535. *
  536. * @param s The stream from which the data is to be read. The type must support
  537. * the SyncReadStream concept.
  538. *
  539. * @param buffers A dynamic buffer sequence into which the data will be read.
  540. *
  541. * @param match_condition The function object to be called to determine whether
  542. * a match exists. The signature of the function object must be:
  543. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  544. * @endcode
  545. * where @c iterator represents the type:
  546. * @code buffers_iterator<DynamicBuffer_v1::const_buffers_type>
  547. * @endcode
  548. * The iterator parameters @c begin and @c end define the range of bytes to be
  549. * scanned to determine whether there is a match. The @c first member of the
  550. * return value is an iterator marking one-past-the-end of the bytes that have
  551. * been consumed by the match function. This iterator is used to calculate the
  552. * @c begin parameter for any subsequent invocation of the match condition. The
  553. * @c second member of the return value is true if a match has been found, false
  554. * otherwise.
  555. *
  556. * @param ec Set to indicate what error occurred, if any.
  557. *
  558. * @returns The number of bytes in the dynamic buffer sequence's get area that
  559. * have been fully consumed by the match function. Returns 0 if an error
  560. * occurred.
  561. *
  562. * @note After a successful read_until operation, the dynamic buffer sequence
  563. * may contain additional data beyond that which matched the function object.
  564. * An application will typically leave that data in the dynamic buffer sequence
  565. * for a subsequent read_until operation to examine.
  566. *
  567. * @note The default implementation of the @c is_match_condition type trait
  568. * evaluates to true for function pointers and function objects with a
  569. * @c result_type typedef. It must be specialised for other user-defined
  570. * function objects.
  571. */
  572. template <typename SyncReadStream,
  573. typename DynamicBuffer_v1, typename MatchCondition>
  574. std::size_t read_until(SyncReadStream& s,
  575. DynamicBuffer_v1&& buffers,
  576. MatchCondition match_condition, boost::system::error_code& ec,
  577. constraint_t<
  578. is_match_condition<MatchCondition>::value
  579. > = 0,
  580. constraint_t<
  581. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  582. > = 0,
  583. constraint_t<
  584. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  585. > = 0);
  586. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  587. /// Read data into a streambuf until it contains a specified delimiter.
  588. /**
  589. * This function is used to read data into the specified streambuf until the
  590. * streambuf's get area contains the specified delimiter. The call will block
  591. * until one of the following conditions is true:
  592. *
  593. * @li The get area of the streambuf contains the specified delimiter.
  594. *
  595. * @li An error occurred.
  596. *
  597. * This operation is implemented in terms of zero or more calls to the stream's
  598. * read_some function. If the streambuf's get area already contains the
  599. * delimiter, the function returns immediately.
  600. *
  601. * @param s The stream from which the data is to be read. The type must support
  602. * the SyncReadStream concept.
  603. *
  604. * @param b A streambuf object into which the data will be read.
  605. *
  606. * @param delim The delimiter character.
  607. *
  608. * @returns The number of bytes in the streambuf's get area up to and including
  609. * the delimiter.
  610. *
  611. * @throws boost::system::system_error Thrown on failure.
  612. *
  613. * @note After a successful read_until operation, the streambuf may contain
  614. * additional data beyond the delimiter. An application will typically leave
  615. * that data in the streambuf for a subsequent read_until operation to examine.
  616. *
  617. * @par Example
  618. * To read data into a streambuf until a newline is encountered:
  619. * @code boost::asio::streambuf b;
  620. * boost::asio::read_until(s, b, '\n');
  621. * std::istream is(&b);
  622. * std::string line;
  623. * std::getline(is, line); @endcode
  624. * After the @c read_until operation completes successfully, the buffer @c b
  625. * contains the delimiter:
  626. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  627. * The call to @c std::getline then extracts the data up to and including the
  628. * newline (which is discarded), so that the string @c line contains:
  629. * @code { 'a', 'b', ..., 'c' } @endcode
  630. * The remaining data is left in the buffer @c b as follows:
  631. * @code { 'd', 'e', ... } @endcode
  632. * This data may be the start of a new line, to be extracted by a subsequent
  633. * @c read_until operation.
  634. */
  635. template <typename SyncReadStream, typename Allocator>
  636. std::size_t read_until(SyncReadStream& s,
  637. boost::asio::basic_streambuf<Allocator>& b, char delim);
  638. /// Read data into a streambuf until it contains a specified delimiter.
  639. /**
  640. * This function is used to read data into the specified streambuf until the
  641. * streambuf's get area contains the specified delimiter. The call will block
  642. * until one of the following conditions is true:
  643. *
  644. * @li The get area of the streambuf contains the specified delimiter.
  645. *
  646. * @li An error occurred.
  647. *
  648. * This operation is implemented in terms of zero or more calls to the stream's
  649. * read_some function. If the streambuf's get area already contains the
  650. * delimiter, the function returns immediately.
  651. *
  652. * @param s The stream from which the data is to be read. The type must support
  653. * the SyncReadStream concept.
  654. *
  655. * @param b A streambuf object into which the data will be read.
  656. *
  657. * @param delim The delimiter character.
  658. *
  659. * @param ec Set to indicate what error occurred, if any.
  660. *
  661. * @returns The number of bytes in the streambuf's get area up to and including
  662. * the delimiter. Returns 0 if an error occurred.
  663. *
  664. * @note After a successful read_until operation, the streambuf may contain
  665. * additional data beyond the delimiter. An application will typically leave
  666. * that data in the streambuf for a subsequent read_until operation to examine.
  667. */
  668. template <typename SyncReadStream, typename Allocator>
  669. std::size_t read_until(SyncReadStream& s,
  670. boost::asio::basic_streambuf<Allocator>& b, char delim,
  671. boost::system::error_code& ec);
  672. /// Read data into a streambuf until it contains a specified delimiter.
  673. /**
  674. * This function is used to read data into the specified streambuf until the
  675. * streambuf's get area contains the specified delimiter. The call will block
  676. * until one of the following conditions is true:
  677. *
  678. * @li The get area of the streambuf contains the specified delimiter.
  679. *
  680. * @li An error occurred.
  681. *
  682. * This operation is implemented in terms of zero or more calls to the stream's
  683. * read_some function. If the streambuf's get area already contains the
  684. * delimiter, the function returns immediately.
  685. *
  686. * @param s The stream from which the data is to be read. The type must support
  687. * the SyncReadStream concept.
  688. *
  689. * @param b A streambuf object into which the data will be read.
  690. *
  691. * @param delim The delimiter string.
  692. *
  693. * @returns The number of bytes in the streambuf's get area up to and including
  694. * the delimiter.
  695. *
  696. * @throws boost::system::system_error Thrown on failure.
  697. *
  698. * @note After a successful read_until operation, the streambuf may contain
  699. * additional data beyond the delimiter. An application will typically leave
  700. * that data in the streambuf for a subsequent read_until operation to examine.
  701. *
  702. * @par Example
  703. * To read data into a streambuf until a newline is encountered:
  704. * @code boost::asio::streambuf b;
  705. * boost::asio::read_until(s, b, "\r\n");
  706. * std::istream is(&b);
  707. * std::string line;
  708. * std::getline(is, line); @endcode
  709. * After the @c read_until operation completes successfully, the buffer @c b
  710. * contains the delimiter:
  711. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  712. * The call to @c std::getline then extracts the data up to and including the
  713. * newline (which is discarded), so that the string @c line contains:
  714. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  715. * The remaining data is left in the buffer @c b as follows:
  716. * @code { 'd', 'e', ... } @endcode
  717. * This data may be the start of a new line, to be extracted by a subsequent
  718. * @c read_until operation.
  719. */
  720. template <typename SyncReadStream, typename Allocator>
  721. std::size_t read_until(SyncReadStream& s,
  722. boost::asio::basic_streambuf<Allocator>& b,
  723. BOOST_ASIO_STRING_VIEW_PARAM delim);
  724. /// Read data into a streambuf until it contains a specified delimiter.
  725. /**
  726. * This function is used to read data into the specified streambuf until the
  727. * streambuf's get area contains the specified delimiter. The call will block
  728. * until one of the following conditions is true:
  729. *
  730. * @li The get area of the streambuf contains the specified delimiter.
  731. *
  732. * @li An error occurred.
  733. *
  734. * This operation is implemented in terms of zero or more calls to the stream's
  735. * read_some function. If the streambuf's get area already contains the
  736. * delimiter, the function returns immediately.
  737. *
  738. * @param s The stream from which the data is to be read. The type must support
  739. * the SyncReadStream concept.
  740. *
  741. * @param b A streambuf object into which the data will be read.
  742. *
  743. * @param delim The delimiter string.
  744. *
  745. * @param ec Set to indicate what error occurred, if any.
  746. *
  747. * @returns The number of bytes in the streambuf's get area up to and including
  748. * the delimiter. Returns 0 if an error occurred.
  749. *
  750. * @note After a successful read_until operation, the streambuf may contain
  751. * additional data beyond the delimiter. An application will typically leave
  752. * that data in the streambuf for a subsequent read_until operation to examine.
  753. */
  754. template <typename SyncReadStream, typename Allocator>
  755. std::size_t read_until(SyncReadStream& s,
  756. boost::asio::basic_streambuf<Allocator>& b,
  757. BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec);
  758. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  759. || defined(GENERATING_DOCUMENTATION)
  760. /// Read data into a streambuf until some part of the data it contains matches
  761. /// a regular expression.
  762. /**
  763. * This function is used to read data into the specified streambuf until the
  764. * streambuf's get area contains some data that matches a regular expression.
  765. * The call will block until one of the following conditions is true:
  766. *
  767. * @li A substring of the streambuf's get area matches the regular expression.
  768. *
  769. * @li An error occurred.
  770. *
  771. * This operation is implemented in terms of zero or more calls to the stream's
  772. * read_some function. If the streambuf's get area already contains data that
  773. * matches the regular expression, the function returns immediately.
  774. *
  775. * @param s The stream from which the data is to be read. The type must support
  776. * the SyncReadStream concept.
  777. *
  778. * @param b A streambuf object into which the data will be read.
  779. *
  780. * @param expr The regular expression.
  781. *
  782. * @returns The number of bytes in the streambuf's get area up to and including
  783. * the substring that matches the regular expression.
  784. *
  785. * @throws boost::system::system_error Thrown on failure.
  786. *
  787. * @note After a successful read_until operation, the streambuf may contain
  788. * additional data beyond that which matched the regular expression. An
  789. * application will typically leave that data in the streambuf for a subsequent
  790. * read_until operation to examine.
  791. *
  792. * @par Example
  793. * To read data into a streambuf until a CR-LF sequence is encountered:
  794. * @code boost::asio::streambuf b;
  795. * boost::asio::read_until(s, b, boost::regex("\r\n"));
  796. * std::istream is(&b);
  797. * std::string line;
  798. * std::getline(is, line); @endcode
  799. * After the @c read_until operation completes successfully, the buffer @c b
  800. * contains the data which matched the regular expression:
  801. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  802. * The call to @c std::getline then extracts the data up to and including the
  803. * newline (which is discarded), so that the string @c line contains:
  804. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  805. * The remaining data is left in the buffer @c b as follows:
  806. * @code { 'd', 'e', ... } @endcode
  807. * This data may be the start of a new line, to be extracted by a subsequent
  808. * @c read_until operation.
  809. */
  810. template <typename SyncReadStream, typename Allocator, typename Traits>
  811. std::size_t read_until(SyncReadStream& s,
  812. boost::asio::basic_streambuf<Allocator>& b,
  813. const boost::basic_regex<char, Traits>& expr);
  814. /// Read data into a streambuf until some part of the data it contains matches
  815. /// a regular expression.
  816. /**
  817. * This function is used to read data into the specified streambuf until the
  818. * streambuf's get area contains some data that matches a regular expression.
  819. * The call will block until one of the following conditions is true:
  820. *
  821. * @li A substring of the streambuf's get area matches the regular expression.
  822. *
  823. * @li An error occurred.
  824. *
  825. * This operation is implemented in terms of zero or more calls to the stream's
  826. * read_some function. If the streambuf's get area already contains data that
  827. * matches the regular expression, the function returns immediately.
  828. *
  829. * @param s The stream from which the data is to be read. The type must support
  830. * the SyncReadStream concept.
  831. *
  832. * @param b A streambuf object into which the data will be read.
  833. *
  834. * @param expr The regular expression.
  835. *
  836. * @param ec Set to indicate what error occurred, if any.
  837. *
  838. * @returns The number of bytes in the streambuf's get area up to and including
  839. * the substring that matches the regular expression. Returns 0 if an error
  840. * occurred.
  841. *
  842. * @note After a successful read_until operation, the streambuf may contain
  843. * additional data beyond that which matched the regular expression. An
  844. * application will typically leave that data in the streambuf for a subsequent
  845. * read_until operation to examine.
  846. */
  847. template <typename SyncReadStream, typename Allocator, typename Traits>
  848. std::size_t read_until(SyncReadStream& s,
  849. boost::asio::basic_streambuf<Allocator>& b,
  850. const boost::basic_regex<char, Traits>& expr,
  851. boost::system::error_code& ec);
  852. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  853. // || defined(GENERATING_DOCUMENTATION)
  854. /// Read data into a streambuf until a function object indicates a match.
  855. /**
  856. * This function is used to read data into the specified streambuf until a
  857. * user-defined match condition function object, when applied to the data
  858. * contained in the streambuf, indicates a successful match. The call will
  859. * block until one of the following conditions is true:
  860. *
  861. * @li The match condition function object returns a std::pair where the second
  862. * element evaluates to true.
  863. *
  864. * @li An error occurred.
  865. *
  866. * This operation is implemented in terms of zero or more calls to the stream's
  867. * read_some function. If the match condition function object already indicates
  868. * a match, the function returns immediately.
  869. *
  870. * @param s The stream from which the data is to be read. The type must support
  871. * the SyncReadStream concept.
  872. *
  873. * @param b A streambuf object into which the data will be read.
  874. *
  875. * @param match_condition The function object to be called to determine whether
  876. * a match exists. The signature of the function object must be:
  877. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  878. * @endcode
  879. * where @c iterator represents the type:
  880. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  881. * @endcode
  882. * The iterator parameters @c begin and @c end define the range of bytes to be
  883. * scanned to determine whether there is a match. The @c first member of the
  884. * return value is an iterator marking one-past-the-end of the bytes that have
  885. * been consumed by the match function. This iterator is used to calculate the
  886. * @c begin parameter for any subsequent invocation of the match condition. The
  887. * @c second member of the return value is true if a match has been found, false
  888. * otherwise.
  889. *
  890. * @returns The number of bytes in the streambuf's get area that have been fully
  891. * consumed by the match function.
  892. *
  893. * @throws boost::system::system_error Thrown on failure.
  894. *
  895. * @note After a successful read_until operation, the streambuf may contain
  896. * additional data beyond that which matched the function object. An application
  897. * will typically leave that data in the streambuf for a subsequent read_until
  898. * operation to examine.
  899. *
  900. * @note The default implementation of the @c is_match_condition type trait
  901. * evaluates to true for function pointers and function objects with a
  902. * @c result_type typedef. It must be specialised for other user-defined
  903. * function objects.
  904. *
  905. * @par Examples
  906. * To read data into a streambuf until whitespace is encountered:
  907. * @code typedef boost::asio::buffers_iterator<
  908. * boost::asio::streambuf::const_buffers_type> iterator;
  909. *
  910. * std::pair<iterator, bool>
  911. * match_whitespace(iterator begin, iterator end)
  912. * {
  913. * iterator i = begin;
  914. * while (i != end)
  915. * if (std::isspace(*i++))
  916. * return std::make_pair(i, true);
  917. * return std::make_pair(i, false);
  918. * }
  919. * ...
  920. * boost::asio::streambuf b;
  921. * boost::asio::read_until(s, b, match_whitespace);
  922. * @endcode
  923. *
  924. * To read data into a streambuf until a matching character is found:
  925. * @code class match_char
  926. * {
  927. * public:
  928. * explicit match_char(char c) : c_(c) {}
  929. *
  930. * template <typename Iterator>
  931. * std::pair<Iterator, bool> operator()(
  932. * Iterator begin, Iterator end) const
  933. * {
  934. * Iterator i = begin;
  935. * while (i != end)
  936. * if (c_ == *i++)
  937. * return std::make_pair(i, true);
  938. * return std::make_pair(i, false);
  939. * }
  940. *
  941. * private:
  942. * char c_;
  943. * };
  944. *
  945. * namespace asio {
  946. * template <> struct is_match_condition<match_char>
  947. * : public boost::true_type {};
  948. * } // namespace asio
  949. * ...
  950. * boost::asio::streambuf b;
  951. * boost::asio::read_until(s, b, match_char('a'));
  952. * @endcode
  953. */
  954. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  955. std::size_t read_until(SyncReadStream& s,
  956. boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
  957. constraint_t<is_match_condition<MatchCondition>::value> = 0);
  958. /// Read data into a streambuf until a function object indicates a match.
  959. /**
  960. * This function is used to read data into the specified streambuf until a
  961. * user-defined match condition function object, when applied to the data
  962. * contained in the streambuf, indicates a successful match. The call will
  963. * block until one of the following conditions is true:
  964. *
  965. * @li The match condition function object returns a std::pair where the second
  966. * element evaluates to true.
  967. *
  968. * @li An error occurred.
  969. *
  970. * This operation is implemented in terms of zero or more calls to the stream's
  971. * read_some function. If the match condition function object already indicates
  972. * a match, the function returns immediately.
  973. *
  974. * @param s The stream from which the data is to be read. The type must support
  975. * the SyncReadStream concept.
  976. *
  977. * @param b A streambuf object into which the data will be read.
  978. *
  979. * @param match_condition The function object to be called to determine whether
  980. * a match exists. The signature of the function object must be:
  981. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  982. * @endcode
  983. * where @c iterator represents the type:
  984. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  985. * @endcode
  986. * The iterator parameters @c begin and @c end define the range of bytes to be
  987. * scanned to determine whether there is a match. The @c first member of the
  988. * return value is an iterator marking one-past-the-end of the bytes that have
  989. * been consumed by the match function. This iterator is used to calculate the
  990. * @c begin parameter for any subsequent invocation of the match condition. The
  991. * @c second member of the return value is true if a match has been found, false
  992. * otherwise.
  993. *
  994. * @param ec Set to indicate what error occurred, if any.
  995. *
  996. * @returns The number of bytes in the streambuf's get area that have been fully
  997. * consumed by the match function. Returns 0 if an error occurred.
  998. *
  999. * @note After a successful read_until operation, the streambuf may contain
  1000. * additional data beyond that which matched the function object. An application
  1001. * will typically leave that data in the streambuf for a subsequent read_until
  1002. * operation to examine.
  1003. *
  1004. * @note The default implementation of the @c is_match_condition type trait
  1005. * evaluates to true for function pointers and function objects with a
  1006. * @c result_type typedef. It must be specialised for other user-defined
  1007. * function objects.
  1008. */
  1009. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  1010. std::size_t read_until(SyncReadStream& s,
  1011. boost::asio::basic_streambuf<Allocator>& b,
  1012. MatchCondition match_condition, boost::system::error_code& ec,
  1013. constraint_t<is_match_condition<MatchCondition>::value> = 0);
  1014. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  1015. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1016. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  1017. /// Read data into a dynamic buffer sequence until it contains a specified
  1018. /// delimiter.
  1019. /**
  1020. * This function is used to read data into the specified dynamic buffer
  1021. * sequence until the dynamic buffer sequence's get area contains the specified
  1022. * delimiter. The call will block until one of the following conditions is
  1023. * true:
  1024. *
  1025. * @li The get area of the dynamic buffer sequence contains the specified
  1026. * delimiter.
  1027. *
  1028. * @li An error occurred.
  1029. *
  1030. * This operation is implemented in terms of zero or more calls to the stream's
  1031. * read_some function. If the dynamic buffer sequence's get area already
  1032. * contains the delimiter, the function returns immediately.
  1033. *
  1034. * @param s The stream from which the data is to be read. The type must support
  1035. * the SyncReadStream concept.
  1036. *
  1037. * @param buffers The dynamic buffer sequence into which the data will be read.
  1038. *
  1039. * @param delim The delimiter character.
  1040. *
  1041. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1042. * and including the delimiter.
  1043. *
  1044. * @throws boost::system::system_error Thrown on failure.
  1045. *
  1046. * @note After a successful read_until operation, the dynamic buffer sequence
  1047. * may contain additional data beyond the delimiter. An application will
  1048. * typically leave that data in the dynamic buffer sequence for a subsequent
  1049. * read_until operation to examine.
  1050. *
  1051. * @par Example
  1052. * To read data into a @c std::string until a newline is encountered:
  1053. * @code std::string data;
  1054. * std::size_t n = boost::asio::read_until(s,
  1055. * boost::asio::dynamic_buffer(data), '\n');
  1056. * std::string line = data.substr(0, n);
  1057. * data.erase(0, n); @endcode
  1058. * After the @c read_until operation completes successfully, the string @c data
  1059. * contains the delimiter:
  1060. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1061. * The call to @c substr then extracts the data up to and including the
  1062. * delimiter, so that the string @c line contains:
  1063. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1064. * After the call to @c erase, the remaining data is left in the buffer @c b as
  1065. * follows:
  1066. * @code { 'd', 'e', ... } @endcode
  1067. * This data may be the start of a new line, to be extracted by a subsequent
  1068. * @c read_until operation.
  1069. */
  1070. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1071. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers, char delim,
  1072. constraint_t<
  1073. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1074. > = 0);
  1075. /// Read data into a dynamic buffer sequence until it contains a specified
  1076. /// delimiter.
  1077. /**
  1078. * This function is used to read data into the specified dynamic buffer
  1079. * sequence until the dynamic buffer sequence's get area contains the specified
  1080. * delimiter. The call will block until one of the following conditions is
  1081. * true:
  1082. *
  1083. * @li The get area of the dynamic buffer sequence contains the specified
  1084. * delimiter.
  1085. *
  1086. * @li An error occurred.
  1087. *
  1088. * This operation is implemented in terms of zero or more calls to the stream's
  1089. * read_some function. If the dynamic buffer sequence's get area already
  1090. * contains the delimiter, the function returns immediately.
  1091. *
  1092. * @param s The stream from which the data is to be read. The type must support
  1093. * the SyncReadStream concept.
  1094. *
  1095. * @param buffers The dynamic buffer sequence into which the data will be read.
  1096. *
  1097. * @param delim The delimiter character.
  1098. *
  1099. * @param ec Set to indicate what error occurred, if any.
  1100. *
  1101. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1102. * and including the delimiter. Returns 0 if an error occurred.
  1103. *
  1104. * @note After a successful read_until operation, the dynamic buffer sequence
  1105. * may contain additional data beyond the delimiter. An application will
  1106. * typically leave that data in the dynamic buffer sequence for a subsequent
  1107. * read_until operation to examine.
  1108. */
  1109. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1110. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1111. char delim, boost::system::error_code& ec,
  1112. constraint_t<
  1113. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1114. > = 0);
  1115. /// Read data into a dynamic buffer sequence until it contains a specified
  1116. /// delimiter.
  1117. /**
  1118. * This function is used to read data into the specified dynamic buffer
  1119. * sequence until the dynamic buffer sequence's get area contains the specified
  1120. * delimiter. The call will block until one of the following conditions is
  1121. * true:
  1122. *
  1123. * @li The get area of the dynamic buffer sequence contains the specified
  1124. * delimiter.
  1125. *
  1126. * @li An error occurred.
  1127. *
  1128. * This operation is implemented in terms of zero or more calls to the stream's
  1129. * read_some function. If the dynamic buffer sequence's get area already
  1130. * contains the delimiter, the function returns immediately.
  1131. *
  1132. * @param s The stream from which the data is to be read. The type must support
  1133. * the SyncReadStream concept.
  1134. *
  1135. * @param buffers The dynamic buffer sequence into which the data will be read.
  1136. *
  1137. * @param delim The delimiter string.
  1138. *
  1139. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1140. * and including the delimiter.
  1141. *
  1142. * @note After a successful read_until operation, the dynamic buffer sequence
  1143. * may contain additional data beyond the delimiter. An application will
  1144. * typically leave that data in the dynamic buffer sequence for a subsequent
  1145. * read_until operation to examine.
  1146. *
  1147. * @par Example
  1148. * To read data into a @c std::string until a CR-LF sequence is encountered:
  1149. * @code std::string data;
  1150. * std::size_t n = boost::asio::read_until(s,
  1151. * boost::asio::dynamic_buffer(data), "\r\n");
  1152. * std::string line = data.substr(0, n);
  1153. * data.erase(0, n); @endcode
  1154. * After the @c read_until operation completes successfully, the string @c data
  1155. * contains the delimiter:
  1156. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1157. * The call to @c substr then extracts the data up to and including the
  1158. * delimiter, so that the string @c line contains:
  1159. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1160. * After the call to @c erase, the remaining data is left in the buffer @c b as
  1161. * follows:
  1162. * @code { 'd', 'e', ... } @endcode
  1163. * This data may be the start of a new line, to be extracted by a subsequent
  1164. * @c read_until operation.
  1165. */
  1166. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1167. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1168. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1169. constraint_t<
  1170. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1171. > = 0);
  1172. /// Read data into a dynamic buffer sequence until it contains a specified
  1173. /// delimiter.
  1174. /**
  1175. * This function is used to read data into the specified dynamic buffer
  1176. * sequence until the dynamic buffer sequence's get area contains the specified
  1177. * delimiter. The call will block until one of the following conditions is
  1178. * true:
  1179. *
  1180. * @li The get area of the dynamic buffer sequence contains the specified
  1181. * delimiter.
  1182. *
  1183. * @li An error occurred.
  1184. *
  1185. * This operation is implemented in terms of zero or more calls to the stream's
  1186. * read_some function. If the dynamic buffer sequence's get area already
  1187. * contains the delimiter, the function returns immediately.
  1188. *
  1189. * @param s The stream from which the data is to be read. The type must support
  1190. * the SyncReadStream concept.
  1191. *
  1192. * @param buffers The dynamic buffer sequence into which the data will be read.
  1193. *
  1194. * @param delim The delimiter string.
  1195. *
  1196. * @param ec Set to indicate what error occurred, if any.
  1197. *
  1198. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1199. * and including the delimiter. Returns 0 if an error occurred.
  1200. *
  1201. * @note After a successful read_until operation, the dynamic buffer sequence
  1202. * may contain additional data beyond the delimiter. An application will
  1203. * typically leave that data in the dynamic buffer sequence for a subsequent
  1204. * read_until operation to examine.
  1205. */
  1206. template <typename SyncReadStream, typename DynamicBuffer_v2>
  1207. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1208. BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec,
  1209. constraint_t<
  1210. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1211. > = 0);
  1212. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1213. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  1214. || defined(GENERATING_DOCUMENTATION)
  1215. /// Read data into a dynamic buffer sequence until some part of the data it
  1216. /// contains matches a regular expression.
  1217. /**
  1218. * This function is used to read data into the specified dynamic buffer
  1219. * sequence until the dynamic buffer sequence's get area contains some data
  1220. * that matches a regular expression. The call will block until one of the
  1221. * following conditions is true:
  1222. *
  1223. * @li A substring of the dynamic buffer sequence's get area matches the
  1224. * regular expression.
  1225. *
  1226. * @li An error occurred.
  1227. *
  1228. * This operation is implemented in terms of zero or more calls to the stream's
  1229. * read_some function. If the dynamic buffer sequence's get area already
  1230. * contains data that matches the regular expression, the function returns
  1231. * immediately.
  1232. *
  1233. * @param s The stream from which the data is to be read. The type must support
  1234. * the SyncReadStream concept.
  1235. *
  1236. * @param buffers A dynamic buffer sequence into which the data will be read.
  1237. *
  1238. * @param expr The regular expression.
  1239. *
  1240. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1241. * and including the substring that matches the regular expression.
  1242. *
  1243. * @throws boost::system::system_error Thrown on failure.
  1244. *
  1245. * @note After a successful read_until operation, the dynamic buffer sequence
  1246. * may contain additional data beyond that which matched the regular
  1247. * expression. An application will typically leave that data in the dynamic
  1248. * buffer sequence for a subsequent read_until operation to examine.
  1249. *
  1250. * @par Example
  1251. * To read data into a @c std::string until a CR-LF sequence is encountered:
  1252. * @code std::string data;
  1253. * std::size_t n = boost::asio::read_until(s,
  1254. * boost::asio::dynamic_buffer(data), boost::regex("\r\n"));
  1255. * std::string line = data.substr(0, n);
  1256. * data.erase(0, n); @endcode
  1257. * After the @c read_until operation completes successfully, the string @c data
  1258. * contains the delimiter:
  1259. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1260. * The call to @c substr then extracts the data up to and including the
  1261. * delimiter, so that the string @c line contains:
  1262. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1263. * After the call to @c erase, the remaining data is left in the buffer @c b as
  1264. * follows:
  1265. * @code { 'd', 'e', ... } @endcode
  1266. * This data may be the start of a new line, to be extracted by a subsequent
  1267. * @c read_until operation.
  1268. */
  1269. template <typename SyncReadStream, typename DynamicBuffer_v2, typename Traits>
  1270. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1271. const boost::basic_regex<char, Traits>& expr,
  1272. constraint_t<
  1273. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1274. > = 0);
  1275. /// Read data into a dynamic buffer sequence until some part of the data it
  1276. /// contains matches a regular expression.
  1277. /**
  1278. * This function is used to read data into the specified dynamic buffer
  1279. * sequence until the dynamic buffer sequence's get area contains some data
  1280. * that matches a regular expression. The call will block until one of the
  1281. * following conditions is true:
  1282. *
  1283. * @li A substring of the dynamic buffer sequence's get area matches the
  1284. * regular expression.
  1285. *
  1286. * @li An error occurred.
  1287. *
  1288. * This operation is implemented in terms of zero or more calls to the stream's
  1289. * read_some function. If the dynamic buffer sequence's get area already
  1290. * contains data that matches the regular expression, the function returns
  1291. * immediately.
  1292. *
  1293. * @param s The stream from which the data is to be read. The type must support
  1294. * the SyncReadStream concept.
  1295. *
  1296. * @param buffers A dynamic buffer sequence into which the data will be read.
  1297. *
  1298. * @param expr The regular expression.
  1299. *
  1300. * @param ec Set to indicate what error occurred, if any.
  1301. *
  1302. * @returns The number of bytes in the dynamic buffer sequence's get area up to
  1303. * and including the substring that matches the regular expression. Returns 0
  1304. * if an error occurred.
  1305. *
  1306. * @note After a successful read_until operation, the dynamic buffer sequence
  1307. * may contain additional data beyond that which matched the regular
  1308. * expression. An application will typically leave that data in the dynamic
  1309. * buffer sequence for a subsequent read_until operation to examine.
  1310. */
  1311. template <typename SyncReadStream, typename DynamicBuffer_v2, typename Traits>
  1312. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1313. const boost::basic_regex<char, Traits>& expr, boost::system::error_code& ec,
  1314. constraint_t<
  1315. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1316. > = 0);
  1317. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1318. // || defined(GENERATING_DOCUMENTATION)
  1319. /// Read data into a dynamic buffer sequence until a function object indicates a
  1320. /// match.
  1321. /**
  1322. * This function is used to read data into the specified dynamic buffer
  1323. * sequence until a user-defined match condition function object, when applied
  1324. * to the data contained in the dynamic buffer sequence, indicates a successful
  1325. * match. The call will block until one of the following conditions is true:
  1326. *
  1327. * @li The match condition function object returns a std::pair where the second
  1328. * element evaluates to true.
  1329. *
  1330. * @li An error occurred.
  1331. *
  1332. * This operation is implemented in terms of zero or more calls to the stream's
  1333. * read_some function. If the match condition function object already indicates
  1334. * a match, the function returns immediately.
  1335. *
  1336. * @param s The stream from which the data is to be read. The type must support
  1337. * the SyncReadStream concept.
  1338. *
  1339. * @param buffers A dynamic buffer sequence into which the data will be read.
  1340. *
  1341. * @param match_condition The function object to be called to determine whether
  1342. * a match exists. The signature of the function object must be:
  1343. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1344. * @endcode
  1345. * where @c iterator represents the type:
  1346. * @code buffers_iterator<typename DynamicBuffer_v2::const_buffers_type>
  1347. * @endcode
  1348. * The iterator parameters @c begin and @c end define the range of bytes to be
  1349. * scanned to determine whether there is a match. The @c first member of the
  1350. * return value is an iterator marking one-past-the-end of the bytes that have
  1351. * been consumed by the match function. This iterator is used to calculate the
  1352. * @c begin parameter for any subsequent invocation of the match condition. The
  1353. * @c second member of the return value is true if a match has been found, false
  1354. * otherwise.
  1355. *
  1356. * @returns The number of bytes in the dynamic_buffer's get area that
  1357. * have been fully consumed by the match function.
  1358. *
  1359. * @throws boost::system::system_error Thrown on failure.
  1360. *
  1361. * @note After a successful read_until operation, the dynamic buffer sequence
  1362. * may contain additional data beyond that which matched the function object.
  1363. * An application will typically leave that data in the dynamic buffer sequence
  1364. * for a subsequent read_until operation to examine.
  1365. * @note The default implementation of the @c is_match_condition type trait
  1366. * evaluates to true for function pointers and function objects with a
  1367. * @c result_type typedef. It must be specialised for other user-defined
  1368. * function objects.
  1369. *
  1370. * @par Examples
  1371. * To read data into a dynamic buffer sequence until whitespace is encountered:
  1372. * @code typedef boost::asio::buffers_iterator<
  1373. * boost::asio::const_buffers_1> iterator;
  1374. *
  1375. * std::pair<iterator, bool>
  1376. * match_whitespace(iterator begin, iterator end)
  1377. * {
  1378. * iterator i = begin;
  1379. * while (i != end)
  1380. * if (std::isspace(*i++))
  1381. * return std::make_pair(i, true);
  1382. * return std::make_pair(i, false);
  1383. * }
  1384. * ...
  1385. * std::string data;
  1386. * boost::asio::read_until(s, data, match_whitespace);
  1387. * @endcode
  1388. *
  1389. * To read data into a @c std::string until a matching character is found:
  1390. * @code class match_char
  1391. * {
  1392. * public:
  1393. * explicit match_char(char c) : c_(c) {}
  1394. *
  1395. * template <typename Iterator>
  1396. * std::pair<Iterator, bool> operator()(
  1397. * Iterator begin, Iterator end) const
  1398. * {
  1399. * Iterator i = begin;
  1400. * while (i != end)
  1401. * if (c_ == *i++)
  1402. * return std::make_pair(i, true);
  1403. * return std::make_pair(i, false);
  1404. * }
  1405. *
  1406. * private:
  1407. * char c_;
  1408. * };
  1409. *
  1410. * namespace asio {
  1411. * template <> struct is_match_condition<match_char>
  1412. * : public boost::true_type {};
  1413. * } // namespace asio
  1414. * ...
  1415. * std::string data;
  1416. * boost::asio::read_until(s, data, match_char('a'));
  1417. * @endcode
  1418. */
  1419. template <typename SyncReadStream,
  1420. typename DynamicBuffer_v2, typename MatchCondition>
  1421. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1422. MatchCondition match_condition,
  1423. constraint_t<
  1424. is_match_condition<MatchCondition>::value
  1425. > = 0,
  1426. constraint_t<
  1427. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1428. > = 0);
  1429. /// Read data into a dynamic buffer sequence until a function object indicates a
  1430. /// match.
  1431. /**
  1432. * This function is used to read data into the specified dynamic buffer
  1433. * sequence until a user-defined match condition function object, when applied
  1434. * to the data contained in the dynamic buffer sequence, indicates a successful
  1435. * match. The call will block until one of the following conditions is true:
  1436. *
  1437. * @li The match condition function object returns a std::pair where the second
  1438. * element evaluates to true.
  1439. *
  1440. * @li An error occurred.
  1441. *
  1442. * This operation is implemented in terms of zero or more calls to the stream's
  1443. * read_some function. If the match condition function object already indicates
  1444. * a match, the function returns immediately.
  1445. *
  1446. * @param s The stream from which the data is to be read. The type must support
  1447. * the SyncReadStream concept.
  1448. *
  1449. * @param buffers A dynamic buffer sequence into which the data will be read.
  1450. *
  1451. * @param match_condition The function object to be called to determine whether
  1452. * a match exists. The signature of the function object must be:
  1453. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1454. * @endcode
  1455. * where @c iterator represents the type:
  1456. * @code buffers_iterator<DynamicBuffer_v2::const_buffers_type>
  1457. * @endcode
  1458. * The iterator parameters @c begin and @c end define the range of bytes to be
  1459. * scanned to determine whether there is a match. The @c first member of the
  1460. * return value is an iterator marking one-past-the-end of the bytes that have
  1461. * been consumed by the match function. This iterator is used to calculate the
  1462. * @c begin parameter for any subsequent invocation of the match condition. The
  1463. * @c second member of the return value is true if a match has been found, false
  1464. * otherwise.
  1465. *
  1466. * @param ec Set to indicate what error occurred, if any.
  1467. *
  1468. * @returns The number of bytes in the dynamic buffer sequence's get area that
  1469. * have been fully consumed by the match function. Returns 0 if an error
  1470. * occurred.
  1471. *
  1472. * @note After a successful read_until operation, the dynamic buffer sequence
  1473. * may contain additional data beyond that which matched the function object.
  1474. * An application will typically leave that data in the dynamic buffer sequence
  1475. * for a subsequent read_until operation to examine.
  1476. *
  1477. * @note The default implementation of the @c is_match_condition type trait
  1478. * evaluates to true for function pointers and function objects with a
  1479. * @c result_type typedef. It must be specialised for other user-defined
  1480. * function objects.
  1481. */
  1482. template <typename SyncReadStream,
  1483. typename DynamicBuffer_v2, typename MatchCondition>
  1484. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  1485. MatchCondition match_condition, boost::system::error_code& ec,
  1486. constraint_t<
  1487. is_match_condition<MatchCondition>::value
  1488. > = 0,
  1489. constraint_t<
  1490. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1491. > = 0);
  1492. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1493. /*@}*/
  1494. /**
  1495. * @defgroup async_read_until boost::asio::async_read_until
  1496. *
  1497. * @brief The @c async_read_until function is a composed asynchronous operation
  1498. * that reads data into a dynamic buffer sequence, or into a streambuf, until
  1499. * it contains a delimiter, matches a regular expression, or a function object
  1500. * indicates a match.
  1501. */
  1502. /*@{*/
  1503. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  1504. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1505. /// until it contains a specified delimiter.
  1506. /**
  1507. * This function is used to asynchronously read data into the specified dynamic
  1508. * buffer sequence until the dynamic buffer sequence's get area contains the
  1509. * specified delimiter. It is an initiating function for an @ref
  1510. * asynchronous_operation, and always returns immediately. The asynchronous
  1511. * operation will continue until one of the following conditions is true:
  1512. *
  1513. * @li The get area of the dynamic buffer sequence contains the specified
  1514. * delimiter.
  1515. *
  1516. * @li An error occurred.
  1517. *
  1518. * This operation is implemented in terms of zero or more calls to the stream's
  1519. * async_read_some function, and is known as a <em>composed operation</em>. If
  1520. * the dynamic buffer sequence's get area already contains the delimiter, this
  1521. * asynchronous operation completes immediately. The program must ensure that
  1522. * the stream performs no other read operations (such as async_read,
  1523. * async_read_until, the stream's async_read_some function, or any other
  1524. * composed operations that perform reads) until this operation completes.
  1525. *
  1526. * @param s The stream from which the data is to be read. The type must support
  1527. * the AsyncReadStream concept.
  1528. *
  1529. * @param buffers The dynamic buffer sequence into which the data will be read.
  1530. * Although the buffers object may be copied as necessary, ownership of the
  1531. * underlying memory blocks is retained by the caller, which must guarantee
  1532. * that they remain valid until the completion handler is called.
  1533. *
  1534. * @param delim The delimiter character.
  1535. *
  1536. * @param token The @ref completion_token that will be used to produce a
  1537. * completion handler, which will be called when the read completes.
  1538. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1539. * @ref yield_context, or a function object with the correct completion
  1540. * signature. The function signature of the completion handler must be:
  1541. * @code void handler(
  1542. * // Result of operation.
  1543. * const boost::system::error_code& error,
  1544. *
  1545. * // The number of bytes in the dynamic buffer sequence's
  1546. * // get area up to and including the delimiter.
  1547. * std::size_t bytes_transferred
  1548. * ); @endcode
  1549. * Regardless of whether the asynchronous operation completes immediately or
  1550. * not, the completion handler will not be invoked from within this function.
  1551. * On immediate completion, invocation of the handler will be performed in a
  1552. * manner equivalent to using boost::asio::post().
  1553. *
  1554. * @par Completion Signature
  1555. * @code void(boost::system::error_code, std::size_t) @endcode
  1556. *
  1557. * @note After a successful async_read_until operation, the dynamic buffer
  1558. * sequence may contain additional data beyond the delimiter. An application
  1559. * will typically leave that data in the dynamic buffer sequence for a
  1560. * subsequent async_read_until operation to examine.
  1561. *
  1562. * @par Example
  1563. * To asynchronously read data into a @c std::string until a newline is
  1564. * encountered:
  1565. * @code std::string data;
  1566. * ...
  1567. * void handler(const boost::system::error_code& e, std::size_t size)
  1568. * {
  1569. * if (!e)
  1570. * {
  1571. * std::string line = data.substr(0, n);
  1572. * data.erase(0, n);
  1573. * ...
  1574. * }
  1575. * }
  1576. * ...
  1577. * boost::asio::async_read_until(s, data, '\n', handler); @endcode
  1578. * After the @c async_read_until operation completes successfully, the buffer
  1579. * @c data contains the delimiter:
  1580. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  1581. * The call to @c substr then extracts the data up to and including the
  1582. * delimiter, so that the string @c line contains:
  1583. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  1584. * After the call to @c erase, the remaining data is left in the buffer @c data
  1585. * as follows:
  1586. * @code { 'd', 'e', ... } @endcode
  1587. * This data may be the start of a new line, to be extracted by a subsequent
  1588. * @c async_read_until operation.
  1589. *
  1590. * @par Per-Operation Cancellation
  1591. * This asynchronous operation supports cancellation for the following
  1592. * boost::asio::cancellation_type values:
  1593. *
  1594. * @li @c cancellation_type::terminal
  1595. *
  1596. * @li @c cancellation_type::partial
  1597. *
  1598. * if they are also supported by the @c AsyncReadStream type's
  1599. * @c async_read_some operation.
  1600. */
  1601. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1602. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1603. std::size_t)) ReadToken = default_completion_token_t<
  1604. typename AsyncReadStream::executor_type>>
  1605. auto async_read_until(AsyncReadStream& s,
  1606. DynamicBuffer_v1&& buffers, char delim,
  1607. ReadToken&& token = default_completion_token_t<
  1608. typename AsyncReadStream::executor_type>(),
  1609. constraint_t<
  1610. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  1611. > = 0,
  1612. constraint_t<
  1613. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  1614. > = 0)
  1615. -> decltype(
  1616. async_initiate<ReadToken,
  1617. void (boost::system::error_code, std::size_t)>(
  1618. declval<detail::initiate_async_read_until_delim_v1<AsyncReadStream>>(),
  1619. token, static_cast<DynamicBuffer_v1&&>(buffers), delim));
  1620. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1621. /// until it contains a specified delimiter.
  1622. /**
  1623. * This function is used to asynchronously read data into the specified dynamic
  1624. * buffer sequence until the dynamic buffer sequence's get area contains the
  1625. * specified delimiter. It is an initiating function for an @ref
  1626. * asynchronous_operation, and always returns immediately. The asynchronous
  1627. * operation will continue until one of the following conditions is true:
  1628. *
  1629. * @li The get area of the dynamic buffer sequence contains the specified
  1630. * delimiter.
  1631. *
  1632. * @li An error occurred.
  1633. *
  1634. * This operation is implemented in terms of zero or more calls to the stream's
  1635. * async_read_some function, and is known as a <em>composed operation</em>. If
  1636. * the dynamic buffer sequence's get area already contains the delimiter, this
  1637. * asynchronous operation completes immediately. The program must ensure that
  1638. * the stream performs no other read operations (such as async_read,
  1639. * async_read_until, the stream's async_read_some function, or any other
  1640. * composed operations that perform reads) until this operation completes.
  1641. *
  1642. * @param s The stream from which the data is to be read. The type must support
  1643. * the AsyncReadStream concept.
  1644. *
  1645. * @param buffers The dynamic buffer sequence into which the data will be read.
  1646. * Although the buffers object may be copied as necessary, ownership of the
  1647. * underlying memory blocks is retained by the caller, which must guarantee
  1648. * that they remain valid until the completion handler is called.
  1649. *
  1650. * @param delim The delimiter string.
  1651. *
  1652. * @param token The @ref completion_token that will be used to produce a
  1653. * completion handler, which will be called when the read completes.
  1654. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1655. * @ref yield_context, or a function object with the correct completion
  1656. * signature. The function signature of the completion handler must be:
  1657. * @code void handler(
  1658. * // Result of operation.
  1659. * const boost::system::error_code& error,
  1660. *
  1661. * // The number of bytes in the dynamic buffer sequence's
  1662. * // get area up to and including the delimiter.
  1663. * std::size_t bytes_transferred
  1664. * ); @endcode
  1665. * Regardless of whether the asynchronous operation completes immediately or
  1666. * not, the completion handler will not be invoked from within this function.
  1667. * On immediate completion, invocation of the handler will be performed in a
  1668. * manner equivalent to using boost::asio::post().
  1669. *
  1670. * @par Completion Signature
  1671. * @code void(boost::system::error_code, std::size_t) @endcode
  1672. *
  1673. * @note After a successful async_read_until operation, the dynamic buffer
  1674. * sequence may contain additional data beyond the delimiter. An application
  1675. * will typically leave that data in the dynamic buffer sequence for a
  1676. * subsequent async_read_until operation to examine.
  1677. *
  1678. * @par Example
  1679. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1680. * encountered:
  1681. * @code std::string data;
  1682. * ...
  1683. * void handler(const boost::system::error_code& e, std::size_t size)
  1684. * {
  1685. * if (!e)
  1686. * {
  1687. * std::string line = data.substr(0, n);
  1688. * data.erase(0, n);
  1689. * ...
  1690. * }
  1691. * }
  1692. * ...
  1693. * boost::asio::async_read_until(s, data, "\r\n", handler); @endcode
  1694. * After the @c async_read_until operation completes successfully, the string
  1695. * @c data contains the delimiter:
  1696. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1697. * The call to @c substr then extracts the data up to and including the
  1698. * delimiter, so that the string @c line contains:
  1699. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1700. * After the call to @c erase, the remaining data is left in the string @c data
  1701. * as follows:
  1702. * @code { 'd', 'e', ... } @endcode
  1703. * This data may be the start of a new line, to be extracted by a subsequent
  1704. * @c async_read_until operation.
  1705. *
  1706. * @par Per-Operation Cancellation
  1707. * This asynchronous operation supports cancellation for the following
  1708. * boost::asio::cancellation_type values:
  1709. *
  1710. * @li @c cancellation_type::terminal
  1711. *
  1712. * @li @c cancellation_type::partial
  1713. *
  1714. * if they are also supported by the @c AsyncReadStream type's
  1715. * @c async_read_some operation.
  1716. */
  1717. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1718. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1719. std::size_t)) ReadToken = default_completion_token_t<
  1720. typename AsyncReadStream::executor_type>>
  1721. auto async_read_until(AsyncReadStream& s,
  1722. DynamicBuffer_v1&& buffers,
  1723. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1724. ReadToken&& token = default_completion_token_t<
  1725. typename AsyncReadStream::executor_type>(),
  1726. constraint_t<
  1727. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  1728. > = 0,
  1729. constraint_t<
  1730. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  1731. > = 0)
  1732. -> decltype(
  1733. async_initiate<ReadToken,
  1734. void (boost::system::error_code, std::size_t)>(
  1735. declval<detail::initiate_async_read_until_delim_string_v1<
  1736. AsyncReadStream>>(),
  1737. token, static_cast<DynamicBuffer_v1&&>(buffers),
  1738. static_cast<std::string>(delim)));
  1739. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1740. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  1741. || defined(GENERATING_DOCUMENTATION)
  1742. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1743. /// until some part of its data matches a regular expression.
  1744. /**
  1745. * This function is used to asynchronously read data into the specified dynamic
  1746. * buffer sequence until the dynamic buffer sequence's get area contains some
  1747. * data that matches a regular expression. It is an initiating function for an
  1748. * @ref asynchronous_operation, and always returns immediately. The
  1749. * asynchronous operation will continue until one of the following conditions
  1750. * is true:
  1751. *
  1752. * @li A substring of the dynamic buffer sequence's get area matches the regular
  1753. * expression.
  1754. *
  1755. * @li An error occurred.
  1756. *
  1757. * This operation is implemented in terms of zero or more calls to the stream's
  1758. * async_read_some function, and is known as a <em>composed operation</em>. If
  1759. * the dynamic buffer sequence's get area already contains data that matches
  1760. * the regular expression, this asynchronous operation completes immediately.
  1761. * The program must ensure that the stream performs no other read operations
  1762. * (such as async_read, async_read_until, the stream's async_read_some
  1763. * function, or any other composed operations that perform reads) until this
  1764. * operation completes.
  1765. *
  1766. * @param s The stream from which the data is to be read. The type must support
  1767. * the AsyncReadStream concept.
  1768. *
  1769. * @param buffers The dynamic buffer sequence into which the data will be read.
  1770. * Although the buffers object may be copied as necessary, ownership of the
  1771. * underlying memory blocks is retained by the caller, which must guarantee
  1772. * that they remain valid until the completion handler is called.
  1773. *
  1774. * @param expr The regular expression.
  1775. *
  1776. * @param token The @ref completion_token that will be used to produce a
  1777. * completion handler, which will be called when the read completes.
  1778. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1779. * @ref yield_context, or a function object with the correct completion
  1780. * signature. The function signature of the completion handler must be:
  1781. * @code void handler(
  1782. * // Result of operation.
  1783. * const boost::system::error_code& error,
  1784. *
  1785. * // The number of bytes in the dynamic buffer
  1786. * // sequence's get area up to and including the
  1787. * // substring that matches the regular expression.
  1788. * // 0 if an error occurred.
  1789. * std::size_t bytes_transferred
  1790. * ); @endcode
  1791. * Regardless of whether the asynchronous operation completes immediately or
  1792. * not, the completion handler will not be invoked from within this function.
  1793. * On immediate completion, invocation of the handler will be performed in a
  1794. * manner equivalent to using boost::asio::post().
  1795. *
  1796. * @par Completion Signature
  1797. * @code void(boost::system::error_code, std::size_t) @endcode
  1798. *
  1799. * @note After a successful async_read_until operation, the dynamic buffer
  1800. * sequence may contain additional data beyond that which matched the regular
  1801. * expression. An application will typically leave that data in the dynamic
  1802. * buffer sequence for a subsequent async_read_until operation to examine.
  1803. *
  1804. * @par Example
  1805. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  1806. * encountered:
  1807. * @code std::string data;
  1808. * ...
  1809. * void handler(const boost::system::error_code& e, std::size_t size)
  1810. * {
  1811. * if (!e)
  1812. * {
  1813. * std::string line = data.substr(0, n);
  1814. * data.erase(0, n);
  1815. * ...
  1816. * }
  1817. * }
  1818. * ...
  1819. * boost::asio::async_read_until(s, data,
  1820. * boost::regex("\r\n"), handler); @endcode
  1821. * After the @c async_read_until operation completes successfully, the string
  1822. * @c data contains the data which matched the regular expression:
  1823. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  1824. * The call to @c substr then extracts the data up to and including the match,
  1825. * so that the string @c line contains:
  1826. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  1827. * After the call to @c erase, the remaining data is left in the string @c data
  1828. * as follows:
  1829. * @code { 'd', 'e', ... } @endcode
  1830. * This data may be the start of a new line, to be extracted by a subsequent
  1831. * @c async_read_until operation.
  1832. *
  1833. * @par Per-Operation Cancellation
  1834. * This asynchronous operation supports cancellation for the following
  1835. * boost::asio::cancellation_type values:
  1836. *
  1837. * @li @c cancellation_type::terminal
  1838. *
  1839. * @li @c cancellation_type::partial
  1840. *
  1841. * if they are also supported by the @c AsyncReadStream type's
  1842. * @c async_read_some operation.
  1843. */
  1844. template <typename AsyncReadStream, typename DynamicBuffer_v1, typename Traits,
  1845. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1846. std::size_t)) ReadToken = default_completion_token_t<
  1847. typename AsyncReadStream::executor_type>>
  1848. auto async_read_until(AsyncReadStream& s, DynamicBuffer_v1&& buffers,
  1849. const boost::basic_regex<char, Traits>& expr,
  1850. ReadToken&& token = default_completion_token_t<
  1851. typename AsyncReadStream::executor_type>(),
  1852. constraint_t<
  1853. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  1854. > = 0,
  1855. constraint_t<
  1856. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  1857. > = 0)
  1858. -> decltype(
  1859. async_initiate<ReadToken,
  1860. void (boost::system::error_code, std::size_t)>(
  1861. declval<detail::initiate_async_read_until_expr_v1<AsyncReadStream>>(),
  1862. token, static_cast<DynamicBuffer_v1&&>(buffers), expr));
  1863. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1864. // || defined(GENERATING_DOCUMENTATION)
  1865. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  1866. /// until a function object indicates a match.
  1867. /**
  1868. * This function is used to asynchronously read data into the specified dynamic
  1869. * buffer sequence until a user-defined match condition function object, when
  1870. * applied to the data contained in the dynamic buffer sequence, indicates a
  1871. * successful match. It is an initiating function for an @ref
  1872. * asynchronous_operation, and always returns immediately. The asynchronous
  1873. * operation will continue until one of the following conditions is true:
  1874. *
  1875. * @li The match condition function object returns a std::pair where the second
  1876. * element evaluates to true.
  1877. *
  1878. * @li An error occurred.
  1879. *
  1880. * This operation is implemented in terms of zero or more calls to the stream's
  1881. * async_read_some function, and is known as a <em>composed operation</em>. If
  1882. * the match condition function object already indicates a match, this
  1883. * asynchronous operation completes immediately. The program must ensure that
  1884. * the stream performs no other read operations (such as async_read,
  1885. * async_read_until, the stream's async_read_some function, or any other
  1886. * composed operations that perform reads) until this operation completes.
  1887. *
  1888. * @param s The stream from which the data is to be read. The type must support
  1889. * the AsyncReadStream concept.
  1890. *
  1891. * @param buffers The dynamic buffer sequence into which the data will be read.
  1892. * Although the buffers object may be copied as necessary, ownership of the
  1893. * underlying memory blocks is retained by the caller, which must guarantee
  1894. * that they remain valid until the completion handler is called.
  1895. *
  1896. * @param match_condition The function object to be called to determine whether
  1897. * a match exists. The signature of the function object must be:
  1898. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  1899. * @endcode
  1900. * where @c iterator represents the type:
  1901. * @code buffers_iterator<typename DynamicBuffer_v1::const_buffers_type>
  1902. * @endcode
  1903. * The iterator parameters @c begin and @c end define the range of bytes to be
  1904. * scanned to determine whether there is a match. The @c first member of the
  1905. * return value is an iterator marking one-past-the-end of the bytes that have
  1906. * been consumed by the match function. This iterator is used to calculate the
  1907. * @c begin parameter for any subsequent invocation of the match condition. The
  1908. * @c second member of the return value is true if a match has been found, false
  1909. * otherwise.
  1910. *
  1911. * @param token The @ref completion_token that will be used to produce a
  1912. * completion handler, which will be called when the read completes.
  1913. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1914. * @ref yield_context, or a function object with the correct completion
  1915. * signature. The function signature of the completion handler must be:
  1916. * @code void handler(
  1917. * // Result of operation.
  1918. * const boost::system::error_code& error,
  1919. *
  1920. * // The number of bytes in the dynamic buffer sequence's
  1921. * // get area that have been fully consumed by the match
  1922. * // function. O if an error occurred.
  1923. * std::size_t bytes_transferred
  1924. * ); @endcode
  1925. * Regardless of whether the asynchronous operation completes immediately or
  1926. * not, the completion handler will not be invoked from within this function.
  1927. * On immediate completion, invocation of the handler will be performed in a
  1928. * manner equivalent to using boost::asio::post().
  1929. *
  1930. * @note After a successful async_read_until operation, the dynamic buffer
  1931. * sequence may contain additional data beyond that which matched the function
  1932. * object. An application will typically leave that data in the dynamic buffer
  1933. * sequence for a subsequent async_read_until operation to examine.
  1934. *
  1935. * @par Completion Signature
  1936. * @code void(boost::system::error_code, std::size_t) @endcode
  1937. *
  1938. * @note The default implementation of the @c is_match_condition type trait
  1939. * evaluates to true for function pointers and function objects with a
  1940. * @c result_type typedef. It must be specialised for other user-defined
  1941. * function objects.
  1942. *
  1943. * @par Examples
  1944. * To asynchronously read data into a @c std::string until whitespace is
  1945. * encountered:
  1946. * @code typedef boost::asio::buffers_iterator<
  1947. * boost::asio::const_buffers_1> iterator;
  1948. *
  1949. * std::pair<iterator, bool>
  1950. * match_whitespace(iterator begin, iterator end)
  1951. * {
  1952. * iterator i = begin;
  1953. * while (i != end)
  1954. * if (std::isspace(*i++))
  1955. * return std::make_pair(i, true);
  1956. * return std::make_pair(i, false);
  1957. * }
  1958. * ...
  1959. * void handler(const boost::system::error_code& e, std::size_t size);
  1960. * ...
  1961. * std::string data;
  1962. * boost::asio::async_read_until(s, data, match_whitespace, handler);
  1963. * @endcode
  1964. *
  1965. * To asynchronously read data into a @c std::string until a matching character
  1966. * is found:
  1967. * @code class match_char
  1968. * {
  1969. * public:
  1970. * explicit match_char(char c) : c_(c) {}
  1971. *
  1972. * template <typename Iterator>
  1973. * std::pair<Iterator, bool> operator()(
  1974. * Iterator begin, Iterator end) const
  1975. * {
  1976. * Iterator i = begin;
  1977. * while (i != end)
  1978. * if (c_ == *i++)
  1979. * return std::make_pair(i, true);
  1980. * return std::make_pair(i, false);
  1981. * }
  1982. *
  1983. * private:
  1984. * char c_;
  1985. * };
  1986. *
  1987. * namespace asio {
  1988. * template <> struct is_match_condition<match_char>
  1989. * : public boost::true_type {};
  1990. * } // namespace asio
  1991. * ...
  1992. * void handler(const boost::system::error_code& e, std::size_t size);
  1993. * ...
  1994. * std::string data;
  1995. * boost::asio::async_read_until(s, data, match_char('a'), handler);
  1996. * @endcode
  1997. *
  1998. * @par Per-Operation Cancellation
  1999. * This asynchronous operation supports cancellation for the following
  2000. * boost::asio::cancellation_type values:
  2001. *
  2002. * @li @c cancellation_type::terminal
  2003. *
  2004. * @li @c cancellation_type::partial
  2005. *
  2006. * if they are also supported by the @c AsyncReadStream type's
  2007. * @c async_read_some operation.
  2008. */
  2009. template <typename AsyncReadStream,
  2010. typename DynamicBuffer_v1, typename MatchCondition,
  2011. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2012. std::size_t)) ReadToken = default_completion_token_t<
  2013. typename AsyncReadStream::executor_type>>
  2014. auto async_read_until(AsyncReadStream& s,
  2015. DynamicBuffer_v1&& buffers, MatchCondition match_condition,
  2016. ReadToken&& token = default_completion_token_t<
  2017. typename AsyncReadStream::executor_type>(),
  2018. constraint_t<
  2019. is_match_condition<MatchCondition>::value
  2020. > = 0,
  2021. constraint_t<
  2022. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  2023. > = 0,
  2024. constraint_t<
  2025. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  2026. > = 0)
  2027. -> decltype(
  2028. async_initiate<ReadToken,
  2029. void (boost::system::error_code, std::size_t)>(
  2030. declval<detail::initiate_async_read_until_match_v1<AsyncReadStream>>(),
  2031. token, static_cast<DynamicBuffer_v1&&>(buffers),
  2032. match_condition));
  2033. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  2034. /// Start an asynchronous operation to read data into a streambuf until it
  2035. /// contains a specified delimiter.
  2036. /**
  2037. * This function is used to asynchronously read data into the specified
  2038. * streambuf until the streambuf's get area contains the specified delimiter.
  2039. * It is an initiating function for an @ref asynchronous_operation, and always
  2040. * returns immediately. The asynchronous operation will continue until one of
  2041. * the following conditions is true:
  2042. *
  2043. * @li The get area of the streambuf contains the specified delimiter.
  2044. *
  2045. * @li An error occurred.
  2046. *
  2047. * This operation is implemented in terms of zero or more calls to the stream's
  2048. * async_read_some function, and is known as a <em>composed operation</em>. If
  2049. * the streambuf's get area already contains the delimiter, this asynchronous
  2050. * operation completes immediately. The program must ensure that the stream
  2051. * performs no other read operations (such as async_read, async_read_until, the
  2052. * stream's async_read_some function, or any other composed operations that
  2053. * perform reads) until this operation completes.
  2054. *
  2055. * @param s The stream from which the data is to be read. The type must support
  2056. * the AsyncReadStream concept.
  2057. *
  2058. * @param b A streambuf object into which the data will be read. Ownership of
  2059. * the streambuf is retained by the caller, which must guarantee that it remains
  2060. * valid until the completion handler is called.
  2061. *
  2062. * @param delim The delimiter character.
  2063. *
  2064. * @param token The @ref completion_token that will be used to produce a
  2065. * completion handler, which will be called when the read completes.
  2066. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2067. * @ref yield_context, or a function object with the correct completion
  2068. * signature. The function signature of the completion handler must be:
  2069. * @code void handler(
  2070. * // Result of operation.
  2071. * const boost::system::error_code& error,
  2072. *
  2073. * // The number of bytes in the streambuf's get
  2074. * // area up to and including the delimiter.
  2075. * // 0 if an error occurred.
  2076. * std::size_t bytes_transferred
  2077. * ); @endcode
  2078. * Regardless of whether the asynchronous operation completes immediately or
  2079. * not, the completion handler will not be invoked from within this function.
  2080. * On immediate completion, invocation of the handler will be performed in a
  2081. * manner equivalent to using boost::asio::post().
  2082. *
  2083. * @par Completion Signature
  2084. * @code void(boost::system::error_code, std::size_t) @endcode
  2085. *
  2086. * @note After a successful async_read_until operation, the streambuf may
  2087. * contain additional data beyond the delimiter. An application will typically
  2088. * leave that data in the streambuf for a subsequent async_read_until operation
  2089. * to examine.
  2090. *
  2091. * @par Example
  2092. * To asynchronously read data into a streambuf until a newline is encountered:
  2093. * @code boost::asio::streambuf b;
  2094. * ...
  2095. * void handler(const boost::system::error_code& e, std::size_t size)
  2096. * {
  2097. * if (!e)
  2098. * {
  2099. * std::istream is(&b);
  2100. * std::string line;
  2101. * std::getline(is, line);
  2102. * ...
  2103. * }
  2104. * }
  2105. * ...
  2106. * boost::asio::async_read_until(s, b, '\n', handler); @endcode
  2107. * After the @c async_read_until operation completes successfully, the buffer
  2108. * @c b contains the delimiter:
  2109. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  2110. * The call to @c std::getline then extracts the data up to and including the
  2111. * newline (which is discarded), so that the string @c line contains:
  2112. * @code { 'a', 'b', ..., 'c' } @endcode
  2113. * The remaining data is left in the buffer @c b as follows:
  2114. * @code { 'd', 'e', ... } @endcode
  2115. * This data may be the start of a new line, to be extracted by a subsequent
  2116. * @c async_read_until operation.
  2117. *
  2118. * @par Per-Operation Cancellation
  2119. * This asynchronous operation supports cancellation for the following
  2120. * boost::asio::cancellation_type values:
  2121. *
  2122. * @li @c cancellation_type::terminal
  2123. *
  2124. * @li @c cancellation_type::partial
  2125. *
  2126. * if they are also supported by the @c AsyncReadStream type's
  2127. * @c async_read_some operation.
  2128. */
  2129. template <typename AsyncReadStream, typename Allocator,
  2130. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2131. std::size_t)) ReadToken = default_completion_token_t<
  2132. typename AsyncReadStream::executor_type>>
  2133. auto async_read_until(AsyncReadStream& s,
  2134. boost::asio::basic_streambuf<Allocator>& b, char delim,
  2135. ReadToken&& token = default_completion_token_t<
  2136. typename AsyncReadStream::executor_type>())
  2137. -> decltype(
  2138. async_initiate<ReadToken,
  2139. void (boost::system::error_code, std::size_t)>(
  2140. declval<detail::initiate_async_read_until_delim_v1<AsyncReadStream>>(),
  2141. token, basic_streambuf_ref<Allocator>(b), delim));
  2142. /// Start an asynchronous operation to read data into a streambuf until it
  2143. /// contains a specified delimiter.
  2144. /**
  2145. * This function is used to asynchronously read data into the specified
  2146. * streambuf until the streambuf's get area contains the specified delimiter.
  2147. * It is an initiating function for an @ref asynchronous_operation, and always
  2148. * returns immediately. The asynchronous operation will continue until one of
  2149. * the following conditions is true:
  2150. *
  2151. * @li The get area of the streambuf contains the specified delimiter.
  2152. *
  2153. * @li An error occurred.
  2154. *
  2155. * This operation is implemented in terms of zero or more calls to the stream's
  2156. * async_read_some function, and is known as a <em>composed operation</em>. If
  2157. * the streambuf's get area already contains the delimiter, this asynchronous
  2158. * operation completes immediately. The program must ensure that the stream
  2159. * performs no other read operations (such as async_read, async_read_until, the
  2160. * stream's async_read_some function, or any other composed operations that
  2161. * perform reads) until this operation completes.
  2162. *
  2163. * @param s The stream from which the data is to be read. The type must support
  2164. * the AsyncReadStream concept.
  2165. *
  2166. * @param b A streambuf object into which the data will be read. Ownership of
  2167. * the streambuf is retained by the caller, which must guarantee that it remains
  2168. * valid until the completion handler is called.
  2169. *
  2170. * @param delim The delimiter string.
  2171. *
  2172. * @param token The @ref completion_token that will be used to produce a
  2173. * completion handler, which will be called when the read completes.
  2174. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2175. * @ref yield_context, or a function object with the correct completion
  2176. * signature. The function signature of the completion handler must be:
  2177. * @code void handler(
  2178. * // Result of operation.
  2179. * const boost::system::error_code& error,
  2180. *
  2181. * // The number of bytes in the streambuf's get
  2182. * // area up to and including the delimiter.
  2183. * // 0 if an error occurred.
  2184. * std::size_t bytes_transferred
  2185. * ); @endcode
  2186. * Regardless of whether the asynchronous operation completes immediately or
  2187. * not, the completion handler will not be invoked from within this function.
  2188. * On immediate completion, invocation of the handler will be performed in a
  2189. * manner equivalent to using boost::asio::post().
  2190. *
  2191. * @par Completion Signature
  2192. * @code void(boost::system::error_code, std::size_t) @endcode
  2193. *
  2194. * @note After a successful async_read_until operation, the streambuf may
  2195. * contain additional data beyond the delimiter. An application will typically
  2196. * leave that data in the streambuf for a subsequent async_read_until operation
  2197. * to examine.
  2198. *
  2199. * @par Example
  2200. * To asynchronously read data into a streambuf until a newline is encountered:
  2201. * @code boost::asio::streambuf b;
  2202. * ...
  2203. * void handler(const boost::system::error_code& e, std::size_t size)
  2204. * {
  2205. * if (!e)
  2206. * {
  2207. * std::istream is(&b);
  2208. * std::string line;
  2209. * std::getline(is, line);
  2210. * ...
  2211. * }
  2212. * }
  2213. * ...
  2214. * boost::asio::async_read_until(s, b, "\r\n", handler); @endcode
  2215. * After the @c async_read_until operation completes successfully, the buffer
  2216. * @c b contains the delimiter:
  2217. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2218. * The call to @c std::getline then extracts the data up to and including the
  2219. * newline (which is discarded), so that the string @c line contains:
  2220. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  2221. * The remaining data is left in the buffer @c b as follows:
  2222. * @code { 'd', 'e', ... } @endcode
  2223. * This data may be the start of a new line, to be extracted by a subsequent
  2224. * @c async_read_until operation.
  2225. *
  2226. * @par Per-Operation Cancellation
  2227. * This asynchronous operation supports cancellation for the following
  2228. * boost::asio::cancellation_type values:
  2229. *
  2230. * @li @c cancellation_type::terminal
  2231. *
  2232. * @li @c cancellation_type::partial
  2233. *
  2234. * if they are also supported by the @c AsyncReadStream type's
  2235. * @c async_read_some operation.
  2236. */
  2237. template <typename AsyncReadStream, typename Allocator,
  2238. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2239. std::size_t)) ReadToken = default_completion_token_t<
  2240. typename AsyncReadStream::executor_type>>
  2241. auto async_read_until(AsyncReadStream& s,
  2242. boost::asio::basic_streambuf<Allocator>& b,
  2243. BOOST_ASIO_STRING_VIEW_PARAM delim,
  2244. ReadToken&& token = default_completion_token_t<
  2245. typename AsyncReadStream::executor_type>())
  2246. -> decltype(
  2247. async_initiate<ReadToken,
  2248. void (boost::system::error_code, std::size_t)>(
  2249. declval<detail::initiate_async_read_until_delim_string_v1<
  2250. AsyncReadStream>>(),
  2251. token, basic_streambuf_ref<Allocator>(b),
  2252. static_cast<std::string>(delim)));
  2253. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  2254. || defined(GENERATING_DOCUMENTATION)
  2255. /// Start an asynchronous operation to read data into a streambuf until some
  2256. /// part of its data matches a regular expression.
  2257. /**
  2258. * This function is used to asynchronously read data into the specified
  2259. * streambuf until the streambuf's get area contains some data that matches a
  2260. * regular expression. It is an initiating function for an @ref
  2261. * asynchronous_operation, and always returns immediately. The asynchronous
  2262. * operation will continue until one of the following conditions is true:
  2263. *
  2264. * @li A substring of the streambuf's get area matches the regular expression.
  2265. *
  2266. * @li An error occurred.
  2267. *
  2268. * This operation is implemented in terms of zero or more calls to the stream's
  2269. * async_read_some function, and is known as a <em>composed operation</em>. If
  2270. * the streambuf's get area already contains data that matches the regular
  2271. * expression, this asynchronous operation completes immediately. The program
  2272. * must ensure that the stream performs no other read operations (such as
  2273. * async_read, async_read_until, the stream's async_read_some function, or any
  2274. * other composed operations that perform reads) until this operation
  2275. * completes.
  2276. *
  2277. * @param s The stream from which the data is to be read. The type must support
  2278. * the AsyncReadStream concept.
  2279. *
  2280. * @param b A streambuf object into which the data will be read. Ownership of
  2281. * the streambuf is retained by the caller, which must guarantee that it remains
  2282. * valid until the completion handler is called.
  2283. *
  2284. * @param expr The regular expression.
  2285. *
  2286. * @param token The @ref completion_token that will be used to produce a
  2287. * completion handler, which will be called when the read completes.
  2288. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2289. * @ref yield_context, or a function object with the correct completion
  2290. * signature. The function signature of the completion handler must be:
  2291. * @code void handler(
  2292. * // Result of operation.
  2293. * const boost::system::error_code& error,
  2294. *
  2295. * // The number of bytes in the streambuf's get
  2296. * // area up to and including the substring
  2297. * // that matches the regular. expression.
  2298. * // 0 if an error occurred.
  2299. * std::size_t bytes_transferred
  2300. * ); @endcode
  2301. * Regardless of whether the asynchronous operation completes immediately or
  2302. * not, the completion handler will not be invoked from within this function.
  2303. * On immediate completion, invocation of the handler will be performed in a
  2304. * manner equivalent to using boost::asio::post().
  2305. *
  2306. * @par Completion Signature
  2307. * @code void(boost::system::error_code, std::size_t) @endcode
  2308. *
  2309. * @note After a successful async_read_until operation, the streambuf may
  2310. * contain additional data beyond that which matched the regular expression. An
  2311. * application will typically leave that data in the streambuf for a subsequent
  2312. * async_read_until operation to examine.
  2313. *
  2314. * @par Example
  2315. * To asynchronously read data into a streambuf until a CR-LF sequence is
  2316. * encountered:
  2317. * @code boost::asio::streambuf b;
  2318. * ...
  2319. * void handler(const boost::system::error_code& e, std::size_t size)
  2320. * {
  2321. * if (!e)
  2322. * {
  2323. * std::istream is(&b);
  2324. * std::string line;
  2325. * std::getline(is, line);
  2326. * ...
  2327. * }
  2328. * }
  2329. * ...
  2330. * boost::asio::async_read_until(s, b, boost::regex("\r\n"), handler); @endcode
  2331. * After the @c async_read_until operation completes successfully, the buffer
  2332. * @c b contains the data which matched the regular expression:
  2333. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2334. * The call to @c std::getline then extracts the data up to and including the
  2335. * newline (which is discarded), so that the string @c line contains:
  2336. * @code { 'a', 'b', ..., 'c', '\r' } @endcode
  2337. * The remaining data is left in the buffer @c b as follows:
  2338. * @code { 'd', 'e', ... } @endcode
  2339. * This data may be the start of a new line, to be extracted by a subsequent
  2340. * @c async_read_until operation.
  2341. *
  2342. * @par Per-Operation Cancellation
  2343. * This asynchronous operation supports cancellation for the following
  2344. * boost::asio::cancellation_type values:
  2345. *
  2346. * @li @c cancellation_type::terminal
  2347. *
  2348. * @li @c cancellation_type::partial
  2349. *
  2350. * if they are also supported by the @c AsyncReadStream type's
  2351. * @c async_read_some operation.
  2352. */
  2353. template <typename AsyncReadStream, typename Allocator, typename Traits,
  2354. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2355. std::size_t)) ReadToken = default_completion_token_t<
  2356. typename AsyncReadStream::executor_type>>
  2357. auto async_read_until(AsyncReadStream& s,
  2358. boost::asio::basic_streambuf<Allocator>& b,
  2359. const boost::basic_regex<char, Traits>& expr,
  2360. ReadToken&& token = default_completion_token_t<
  2361. typename AsyncReadStream::executor_type>())
  2362. -> decltype(
  2363. async_initiate<ReadToken,
  2364. void (boost::system::error_code, std::size_t)>(
  2365. declval<detail::initiate_async_read_until_expr_v1<AsyncReadStream>>(),
  2366. token, basic_streambuf_ref<Allocator>(b), expr));
  2367. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  2368. // || defined(GENERATING_DOCUMENTATION)
  2369. /// Start an asynchronous operation to read data into a streambuf until a
  2370. /// function object indicates a match.
  2371. /**
  2372. * This function is used to asynchronously read data into the specified
  2373. * streambuf until a user-defined match condition function object, when applied
  2374. * to the data contained in the streambuf, indicates a successful match. It is
  2375. * an initiating function for an @ref asynchronous_operation, and always
  2376. * returns immediately. The asynchronous operation will continue until one of
  2377. * the following conditions is true:
  2378. *
  2379. * @li The match condition function object returns a std::pair where the second
  2380. * element evaluates to true.
  2381. *
  2382. * @li An error occurred.
  2383. *
  2384. * This operation is implemented in terms of zero or more calls to the stream's
  2385. * async_read_some function, and is known as a <em>composed operation</em>. If
  2386. * the match condition function object already indicates a match, this
  2387. * asynchronous operation completes immediately. The program must ensure that
  2388. * the stream performs no other read operations (such as async_read,
  2389. * async_read_until, the stream's async_read_some function, or any other
  2390. * composed operations that perform reads) until this operation completes.
  2391. *
  2392. * @param s The stream from which the data is to be read. The type must support
  2393. * the AsyncReadStream concept.
  2394. *
  2395. * @param b A streambuf object into which the data will be read.
  2396. *
  2397. * @param match_condition The function object to be called to determine whether
  2398. * a match exists. The signature of the function object must be:
  2399. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  2400. * @endcode
  2401. * where @c iterator represents the type:
  2402. * @code buffers_iterator<basic_streambuf<Allocator>::const_buffers_type>
  2403. * @endcode
  2404. * The iterator parameters @c begin and @c end define the range of bytes to be
  2405. * scanned to determine whether there is a match. The @c first member of the
  2406. * return value is an iterator marking one-past-the-end of the bytes that have
  2407. * been consumed by the match function. This iterator is used to calculate the
  2408. * @c begin parameter for any subsequent invocation of the match condition. The
  2409. * @c second member of the return value is true if a match has been found, false
  2410. * otherwise.
  2411. *
  2412. * @param token The @ref completion_token that will be used to produce a
  2413. * completion handler, which will be called when the read completes.
  2414. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2415. * @ref yield_context, or a function object with the correct completion
  2416. * signature. The function signature of the completion handler must be:
  2417. * @code void handler(
  2418. * // Result of operation.
  2419. * const boost::system::error_code& error,
  2420. *
  2421. * // The number of bytes in the streambuf's get
  2422. * // area that have been fully consumed by the
  2423. * // match function. O if an error occurred.
  2424. * std::size_t bytes_transferred
  2425. * ); @endcode
  2426. * Regardless of whether the asynchronous operation completes immediately or
  2427. * not, the completion handler will not be invoked from within this function.
  2428. * On immediate completion, invocation of the handler will be performed in a
  2429. * manner equivalent to using boost::asio::post().
  2430. *
  2431. * @note After a successful async_read_until operation, the streambuf may
  2432. * contain additional data beyond that which matched the function object. An
  2433. * application will typically leave that data in the streambuf for a subsequent
  2434. * async_read_until operation to examine.
  2435. *
  2436. * @par Completion Signature
  2437. * @code void(boost::system::error_code, std::size_t) @endcode
  2438. *
  2439. * @note The default implementation of the @c is_match_condition type trait
  2440. * evaluates to true for function pointers and function objects with a
  2441. * @c result_type typedef. It must be specialised for other user-defined
  2442. * function objects.
  2443. *
  2444. * @par Examples
  2445. * To asynchronously read data into a streambuf until whitespace is encountered:
  2446. * @code typedef boost::asio::buffers_iterator<
  2447. * boost::asio::streambuf::const_buffers_type> iterator;
  2448. *
  2449. * std::pair<iterator, bool>
  2450. * match_whitespace(iterator begin, iterator end)
  2451. * {
  2452. * iterator i = begin;
  2453. * while (i != end)
  2454. * if (std::isspace(*i++))
  2455. * return std::make_pair(i, true);
  2456. * return std::make_pair(i, false);
  2457. * }
  2458. * ...
  2459. * void handler(const boost::system::error_code& e, std::size_t size);
  2460. * ...
  2461. * boost::asio::streambuf b;
  2462. * boost::asio::async_read_until(s, b, match_whitespace, handler);
  2463. * @endcode
  2464. *
  2465. * To asynchronously read data into a streambuf until a matching character is
  2466. * found:
  2467. * @code class match_char
  2468. * {
  2469. * public:
  2470. * explicit match_char(char c) : c_(c) {}
  2471. *
  2472. * template <typename Iterator>
  2473. * std::pair<Iterator, bool> operator()(
  2474. * Iterator begin, Iterator end) const
  2475. * {
  2476. * Iterator i = begin;
  2477. * while (i != end)
  2478. * if (c_ == *i++)
  2479. * return std::make_pair(i, true);
  2480. * return std::make_pair(i, false);
  2481. * }
  2482. *
  2483. * private:
  2484. * char c_;
  2485. * };
  2486. *
  2487. * namespace asio {
  2488. * template <> struct is_match_condition<match_char>
  2489. * : public boost::true_type {};
  2490. * } // namespace asio
  2491. * ...
  2492. * void handler(const boost::system::error_code& e, std::size_t size);
  2493. * ...
  2494. * boost::asio::streambuf b;
  2495. * boost::asio::async_read_until(s, b, match_char('a'), handler);
  2496. * @endcode
  2497. *
  2498. * @par Per-Operation Cancellation
  2499. * This asynchronous operation supports cancellation for the following
  2500. * boost::asio::cancellation_type values:
  2501. *
  2502. * @li @c cancellation_type::terminal
  2503. *
  2504. * @li @c cancellation_type::partial
  2505. *
  2506. * if they are also supported by the @c AsyncReadStream type's
  2507. * @c async_read_some operation.
  2508. */
  2509. template <typename AsyncReadStream, typename Allocator, typename MatchCondition,
  2510. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2511. std::size_t)) ReadToken = default_completion_token_t<
  2512. typename AsyncReadStream::executor_type>>
  2513. auto async_read_until(AsyncReadStream& s,
  2514. boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
  2515. ReadToken&& token = default_completion_token_t<
  2516. typename AsyncReadStream::executor_type>(),
  2517. constraint_t<is_match_condition<MatchCondition>::value> = 0)
  2518. -> decltype(
  2519. async_initiate<ReadToken,
  2520. void (boost::system::error_code, std::size_t)>(
  2521. declval<detail::initiate_async_read_until_match_v1<AsyncReadStream>>(),
  2522. token, basic_streambuf_ref<Allocator>(b), match_condition));
  2523. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  2524. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  2525. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  2526. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2527. /// until it contains a specified delimiter.
  2528. /**
  2529. * This function is used to asynchronously read data into the specified dynamic
  2530. * buffer sequence until the dynamic buffer sequence's get area contains the
  2531. * specified delimiter. It is an initiating function for an @ref
  2532. * asynchronous_operation, and always returns immediately. The asynchronous
  2533. * operation will continue until one of the following conditions is true:
  2534. *
  2535. * @li The get area of the dynamic buffer sequence contains the specified
  2536. * delimiter.
  2537. *
  2538. * @li An error occurred.
  2539. *
  2540. * This operation is implemented in terms of zero or more calls to the stream's
  2541. * async_read_some function, and is known as a <em>composed operation</em>. If
  2542. * the dynamic buffer sequence's get area already contains the delimiter, this
  2543. * asynchronous operation completes immediately. The program must ensure that
  2544. * the stream performs no other read operations (such as async_read,
  2545. * async_read_until, the stream's async_read_some function, or any other
  2546. * composed operations that perform reads) until this operation completes.
  2547. *
  2548. * @param s The stream from which the data is to be read. The type must support
  2549. * the AsyncReadStream concept.
  2550. *
  2551. * @param buffers The dynamic buffer sequence into which the data will be read.
  2552. * Although the buffers object may be copied as necessary, ownership of the
  2553. * underlying memory blocks is retained by the caller, which must guarantee
  2554. * that they remain valid until the completion handler is called.
  2555. *
  2556. * @param delim The delimiter character.
  2557. *
  2558. * @param token The @ref completion_token that will be used to produce a
  2559. * completion handler, which will be called when the read completes.
  2560. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2561. * @ref yield_context, or a function object with the correct completion
  2562. * signature. The function signature of the completion handler must be:
  2563. * @code void handler(
  2564. * // Result of operation.
  2565. * const boost::system::error_code& error,
  2566. *
  2567. * // The number of bytes in the dynamic buffer sequence's
  2568. * // get area up to and including the delimiter.
  2569. * // 0 if an error occurred.
  2570. * std::size_t bytes_transferred
  2571. * ); @endcode
  2572. * Regardless of whether the asynchronous operation completes immediately or
  2573. * not, the completion handler will not be invoked from within this function.
  2574. * On immediate completion, invocation of the handler will be performed in a
  2575. * manner equivalent to using boost::asio::post().
  2576. *
  2577. * @par Completion Signature
  2578. * @code void(boost::system::error_code, std::size_t) @endcode
  2579. *
  2580. * @note After a successful async_read_until operation, the dynamic buffer
  2581. * sequence may contain additional data beyond the delimiter. An application
  2582. * will typically leave that data in the dynamic buffer sequence for a
  2583. * subsequent async_read_until operation to examine.
  2584. *
  2585. * @par Example
  2586. * To asynchronously read data into a @c std::string until a newline is
  2587. * encountered:
  2588. * @code std::string data;
  2589. * ...
  2590. * void handler(const boost::system::error_code& e, std::size_t size)
  2591. * {
  2592. * if (!e)
  2593. * {
  2594. * std::string line = data.substr(0, n);
  2595. * data.erase(0, n);
  2596. * ...
  2597. * }
  2598. * }
  2599. * ...
  2600. * boost::asio::async_read_until(s, data, '\n', handler); @endcode
  2601. * After the @c async_read_until operation completes successfully, the buffer
  2602. * @c data contains the delimiter:
  2603. * @code { 'a', 'b', ..., 'c', '\n', 'd', 'e', ... } @endcode
  2604. * The call to @c substr then extracts the data up to and including the
  2605. * delimiter, so that the string @c line contains:
  2606. * @code { 'a', 'b', ..., 'c', '\n' } @endcode
  2607. * After the call to @c erase, the remaining data is left in the buffer @c data
  2608. * as follows:
  2609. * @code { 'd', 'e', ... } @endcode
  2610. * This data may be the start of a new line, to be extracted by a subsequent
  2611. * @c async_read_until operation.
  2612. *
  2613. * @par Per-Operation Cancellation
  2614. * This asynchronous operation supports cancellation for the following
  2615. * boost::asio::cancellation_type values:
  2616. *
  2617. * @li @c cancellation_type::terminal
  2618. *
  2619. * @li @c cancellation_type::partial
  2620. *
  2621. * if they are also supported by the @c AsyncReadStream type's
  2622. * @c async_read_some operation.
  2623. */
  2624. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2625. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2626. std::size_t)) ReadToken = default_completion_token_t<
  2627. typename AsyncReadStream::executor_type>>
  2628. auto async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers, char delim,
  2629. ReadToken&& token = default_completion_token_t<
  2630. typename AsyncReadStream::executor_type>(),
  2631. constraint_t<
  2632. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2633. > = 0)
  2634. -> decltype(
  2635. async_initiate<ReadToken,
  2636. void (boost::system::error_code, std::size_t)>(
  2637. declval<detail::initiate_async_read_until_delim_v2<AsyncReadStream>>(),
  2638. token, static_cast<DynamicBuffer_v2&&>(buffers), delim));
  2639. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2640. /// until it contains a specified delimiter.
  2641. /**
  2642. * This function is used to asynchronously read data into the specified dynamic
  2643. * buffer sequence until the dynamic buffer sequence's get area contains the
  2644. * specified delimiter. It is an initiating function for an @ref
  2645. * asynchronous_operation, and always returns immediately. The asynchronous
  2646. * operation will continue until one of the following conditions is true:
  2647. *
  2648. * @li The get area of the dynamic buffer sequence contains the specified
  2649. * delimiter.
  2650. *
  2651. * @li An error occurred.
  2652. *
  2653. * This operation is implemented in terms of zero or more calls to the stream's
  2654. * async_read_some function, and is known as a <em>composed operation</em>. If
  2655. * the dynamic buffer sequence's get area already contains the delimiter, this
  2656. * asynchronous operation completes immediately. The program must ensure that
  2657. * the stream performs no other read operations (such as async_read,
  2658. * async_read_until, the stream's async_read_some function, or any other
  2659. * composed operations that perform reads) until this operation completes.
  2660. *
  2661. * @param s The stream from which the data is to be read. The type must support
  2662. * the AsyncReadStream concept.
  2663. *
  2664. * @param buffers The dynamic buffer sequence into which the data will be read.
  2665. * Although the buffers object may be copied as necessary, ownership of the
  2666. * underlying memory blocks is retained by the caller, which must guarantee
  2667. * that they remain valid until the completion handler is called.
  2668. *
  2669. * @param delim The delimiter string.
  2670. *
  2671. * @param token The @ref completion_token that will be used to produce a
  2672. * completion handler, which will be called when the read completes.
  2673. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2674. * @ref yield_context, or a function object with the correct completion
  2675. * signature. The function signature of the completion handler must be:
  2676. * @code void handler(
  2677. * // Result of operation.
  2678. * const boost::system::error_code& error,
  2679. *
  2680. * // The number of bytes in the dynamic buffer sequence's
  2681. * // get area up to and including the delimiter.
  2682. * std::size_t bytes_transferred
  2683. * ); @endcode
  2684. * Regardless of whether the asynchronous operation completes immediately or
  2685. * not, the completion handler will not be invoked from within this function.
  2686. * On immediate completion, invocation of the handler will be performed in a
  2687. * manner equivalent to using boost::asio::post().
  2688. *
  2689. * @par Completion Signature
  2690. * @code void(boost::system::error_code, std::size_t) @endcode
  2691. *
  2692. * @note After a successful async_read_until operation, the dynamic buffer
  2693. * sequence may contain additional data beyond the delimiter. An application
  2694. * will typically leave that data in the dynamic buffer sequence for a
  2695. * subsequent async_read_until operation to examine.
  2696. *
  2697. * @par Example
  2698. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  2699. * encountered:
  2700. * @code std::string data;
  2701. * ...
  2702. * void handler(const boost::system::error_code& e, std::size_t size)
  2703. * {
  2704. * if (!e)
  2705. * {
  2706. * std::string line = data.substr(0, n);
  2707. * data.erase(0, n);
  2708. * ...
  2709. * }
  2710. * }
  2711. * ...
  2712. * boost::asio::async_read_until(s, data, "\r\n", handler); @endcode
  2713. * After the @c async_read_until operation completes successfully, the string
  2714. * @c data contains the delimiter:
  2715. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2716. * The call to @c substr then extracts the data up to and including the
  2717. * delimiter, so that the string @c line contains:
  2718. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  2719. * After the call to @c erase, the remaining data is left in the string @c data
  2720. * as follows:
  2721. * @code { 'd', 'e', ... } @endcode
  2722. * This data may be the start of a new line, to be extracted by a subsequent
  2723. * @c async_read_until operation.
  2724. *
  2725. * @par Per-Operation Cancellation
  2726. * This asynchronous operation supports cancellation for the following
  2727. * boost::asio::cancellation_type values:
  2728. *
  2729. * @li @c cancellation_type::terminal
  2730. *
  2731. * @li @c cancellation_type::partial
  2732. *
  2733. * if they are also supported by the @c AsyncReadStream type's
  2734. * @c async_read_some operation.
  2735. */
  2736. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2737. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2738. std::size_t)) ReadToken = default_completion_token_t<
  2739. typename AsyncReadStream::executor_type>>
  2740. auto async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2741. BOOST_ASIO_STRING_VIEW_PARAM delim,
  2742. ReadToken&& token = default_completion_token_t<
  2743. typename AsyncReadStream::executor_type>(),
  2744. constraint_t<
  2745. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2746. > = 0)
  2747. -> decltype(
  2748. async_initiate<ReadToken,
  2749. void (boost::system::error_code, std::size_t)>(
  2750. declval<detail::initiate_async_read_until_delim_string_v2<
  2751. AsyncReadStream>>(),
  2752. token, static_cast<DynamicBuffer_v2&&>(buffers),
  2753. static_cast<std::string>(delim)));
  2754. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  2755. #if defined(BOOST_ASIO_HAS_BOOST_REGEX) \
  2756. || defined(GENERATING_DOCUMENTATION)
  2757. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2758. /// until some part of its data matches a regular expression.
  2759. /**
  2760. * This function is used to asynchronously read data into the specified dynamic
  2761. * buffer sequence until the dynamic buffer sequence's get area contains some
  2762. * data that matches a regular expression. It is an initiating function for an
  2763. * @ref asynchronous_operation, and always returns immediately. The
  2764. * asynchronous operation will continue until one of the following conditions
  2765. * is true:
  2766. *
  2767. * @li A substring of the dynamic buffer sequence's get area matches the regular
  2768. * expression.
  2769. *
  2770. * @li An error occurred.
  2771. *
  2772. * This operation is implemented in terms of zero or more calls to the stream's
  2773. * async_read_some function, and is known as a <em>composed operation</em>. If
  2774. * the dynamic buffer sequence's get area already contains data that matches
  2775. * the regular expression, this asynchronous operation completes immediately.
  2776. * The program must ensure that the stream performs no other read operations
  2777. * (such as async_read, async_read_until, the stream's async_read_some
  2778. * function, or any other composed operations that perform reads) until this
  2779. * operation completes.
  2780. *
  2781. * @param s The stream from which the data is to be read. The type must support
  2782. * the AsyncReadStream concept.
  2783. *
  2784. * @param buffers The dynamic buffer sequence into which the data will be read.
  2785. * Although the buffers object may be copied as necessary, ownership of the
  2786. * underlying memory blocks is retained by the caller, which must guarantee
  2787. * that they remain valid until the completion handler is called.
  2788. *
  2789. * @param expr The regular expression.
  2790. *
  2791. * @param token The @ref completion_token that will be used to produce a
  2792. * completion handler, which will be called when the read completes.
  2793. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2794. * @ref yield_context, or a function object with the correct completion
  2795. * signature. The function signature of the completion handler must be:
  2796. * @code void handler(
  2797. * // Result of operation.
  2798. * const boost::system::error_code& error,
  2799. *
  2800. * // The number of bytes in the dynamic buffer
  2801. * // sequence's get area up to and including the
  2802. * // substring that matches the regular expression.
  2803. * // 0 if an error occurred.
  2804. * std::size_t bytes_transferred
  2805. * ); @endcode
  2806. * Regardless of whether the asynchronous operation completes immediately or
  2807. * not, the completion handler will not be invoked from within this function.
  2808. * On immediate completion, invocation of the handler will be performed in a
  2809. * manner equivalent to using boost::asio::post().
  2810. *
  2811. * @par Completion Signature
  2812. * @code void(boost::system::error_code, std::size_t) @endcode
  2813. *
  2814. * @note After a successful async_read_until operation, the dynamic buffer
  2815. * sequence may contain additional data beyond that which matched the regular
  2816. * expression. An application will typically leave that data in the dynamic
  2817. * buffer sequence for a subsequent async_read_until operation to examine.
  2818. *
  2819. * @par Example
  2820. * To asynchronously read data into a @c std::string until a CR-LF sequence is
  2821. * encountered:
  2822. * @code std::string data;
  2823. * ...
  2824. * void handler(const boost::system::error_code& e, std::size_t size)
  2825. * {
  2826. * if (!e)
  2827. * {
  2828. * std::string line = data.substr(0, n);
  2829. * data.erase(0, n);
  2830. * ...
  2831. * }
  2832. * }
  2833. * ...
  2834. * boost::asio::async_read_until(s, data,
  2835. * boost::regex("\r\n"), handler); @endcode
  2836. * After the @c async_read_until operation completes successfully, the string
  2837. * @c data contains the data which matched the regular expression:
  2838. * @code { 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } @endcode
  2839. * The call to @c substr then extracts the data up to and including the match,
  2840. * so that the string @c line contains:
  2841. * @code { 'a', 'b', ..., 'c', '\r', '\n' } @endcode
  2842. * After the call to @c erase, the remaining data is left in the string @c data
  2843. * as follows:
  2844. * @code { 'd', 'e', ... } @endcode
  2845. * This data may be the start of a new line, to be extracted by a subsequent
  2846. * @c async_read_until operation.
  2847. *
  2848. * @par Per-Operation Cancellation
  2849. * This asynchronous operation supports cancellation for the following
  2850. * boost::asio::cancellation_type values:
  2851. *
  2852. * @li @c cancellation_type::terminal
  2853. *
  2854. * @li @c cancellation_type::partial
  2855. *
  2856. * if they are also supported by the @c AsyncReadStream type's
  2857. * @c async_read_some operation.
  2858. */
  2859. template <typename AsyncReadStream, typename DynamicBuffer_v2, typename Traits,
  2860. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  2861. std::size_t)) ReadToken = default_completion_token_t<
  2862. typename AsyncReadStream::executor_type>>
  2863. auto async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2864. const boost::basic_regex<char, Traits>& expr,
  2865. ReadToken&& token = default_completion_token_t<
  2866. typename AsyncReadStream::executor_type>(),
  2867. constraint_t<
  2868. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2869. > = 0)
  2870. -> decltype(
  2871. async_initiate<ReadToken,
  2872. void (boost::system::error_code, std::size_t)>(
  2873. declval<detail::initiate_async_read_until_expr_v2<AsyncReadStream>>(),
  2874. token, static_cast<DynamicBuffer_v2&&>(buffers), expr));
  2875. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  2876. // || defined(GENERATING_DOCUMENTATION)
  2877. /// Start an asynchronous operation to read data into a dynamic buffer sequence
  2878. /// until a function object indicates a match.
  2879. /**
  2880. * This function is used to asynchronously read data into the specified dynamic
  2881. * buffer sequence until a user-defined match condition function object, when
  2882. * applied to the data contained in the dynamic buffer sequence, indicates a
  2883. * successful match. It is an initiating function for an @ref
  2884. * asynchronous_operation, and always returns immediately. The asynchronous
  2885. * operation will continue until one of the following conditions is true:
  2886. *
  2887. * @li The match condition function object returns a std::pair where the second
  2888. * element evaluates to true.
  2889. *
  2890. * @li An error occurred.
  2891. *
  2892. * This operation is implemented in terms of zero or more calls to the stream's
  2893. * async_read_some function, and is known as a <em>composed operation</em>. If
  2894. * the match condition function object already indicates a match, this
  2895. * asynchronous operation completes immediately. The program must ensure that
  2896. * the stream performs no other read operations (such as async_read,
  2897. * async_read_until, the stream's async_read_some function, or any other
  2898. * composed operations that perform reads) until this operation completes.
  2899. *
  2900. * @param s The stream from which the data is to be read. The type must support
  2901. * the AsyncReadStream concept.
  2902. *
  2903. * @param buffers The dynamic buffer sequence into which the data will be read.
  2904. * Although the buffers object may be copied as necessary, ownership of the
  2905. * underlying memory blocks is retained by the caller, which must guarantee
  2906. * that they remain valid until the completion handler is called.
  2907. *
  2908. * @param match_condition The function object to be called to determine whether
  2909. * a match exists. The signature of the function object must be:
  2910. * @code pair<iterator, bool> match_condition(iterator begin, iterator end);
  2911. * @endcode
  2912. * where @c iterator represents the type:
  2913. * @code buffers_iterator<typename DynamicBuffer_v2::const_buffers_type>
  2914. * @endcode
  2915. * The iterator parameters @c begin and @c end define the range of bytes to be
  2916. * scanned to determine whether there is a match. The @c first member of the
  2917. * return value is an iterator marking one-past-the-end of the bytes that have
  2918. * been consumed by the match function. This iterator is used to calculate the
  2919. * @c begin parameter for any subsequent invocation of the match condition. The
  2920. * @c second member of the return value is true if a match has been found, false
  2921. * otherwise.
  2922. *
  2923. * @param token The @ref completion_token that will be used to produce a
  2924. * completion handler, which will be called when the read completes.
  2925. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  2926. * @ref yield_context, or a function object with the correct completion
  2927. * signature. The function signature of the completion handler must be:
  2928. * @code void handler(
  2929. * // Result of operation.
  2930. * const boost::system::error_code& error,
  2931. *
  2932. * // The number of bytes in the dynamic buffer sequence's
  2933. * // get area that have been fully consumed by the match
  2934. * // function. O if an error occurred.
  2935. * std::size_t bytes_transferred
  2936. * ); @endcode
  2937. * Regardless of whether the asynchronous operation completes immediately or
  2938. * not, the completion handler will not be invoked from within this function.
  2939. * On immediate completion, invocation of the handler will be performed in a
  2940. * manner equivalent to using boost::asio::post().
  2941. *
  2942. * @note After a successful async_read_until operation, the dynamic buffer
  2943. * sequence may contain additional data beyond that which matched the function
  2944. * object. An application will typically leave that data in the dynamic buffer
  2945. * sequence for a subsequent async_read_until operation to examine.
  2946. *
  2947. * @par Completion Signature
  2948. * @code void(boost::system::error_code, std::size_t) @endcode
  2949. *
  2950. * @note The default implementation of the @c is_match_condition type trait
  2951. * evaluates to true for function pointers and function objects with a
  2952. * @c result_type typedef. It must be specialised for other user-defined
  2953. * function objects.
  2954. *
  2955. * @par Examples
  2956. * To asynchronously read data into a @c std::string until whitespace is
  2957. * encountered:
  2958. * @code typedef boost::asio::buffers_iterator<
  2959. * boost::asio::const_buffers_1> iterator;
  2960. *
  2961. * std::pair<iterator, bool>
  2962. * match_whitespace(iterator begin, iterator end)
  2963. * {
  2964. * iterator i = begin;
  2965. * while (i != end)
  2966. * if (std::isspace(*i++))
  2967. * return std::make_pair(i, true);
  2968. * return std::make_pair(i, false);
  2969. * }
  2970. * ...
  2971. * void handler(const boost::system::error_code& e, std::size_t size);
  2972. * ...
  2973. * std::string data;
  2974. * boost::asio::async_read_until(s, data, match_whitespace, handler);
  2975. * @endcode
  2976. *
  2977. * To asynchronously read data into a @c std::string until a matching character
  2978. * is found:
  2979. * @code class match_char
  2980. * {
  2981. * public:
  2982. * explicit match_char(char c) : c_(c) {}
  2983. *
  2984. * template <typename Iterator>
  2985. * std::pair<Iterator, bool> operator()(
  2986. * Iterator begin, Iterator end) const
  2987. * {
  2988. * Iterator i = begin;
  2989. * while (i != end)
  2990. * if (c_ == *i++)
  2991. * return std::make_pair(i, true);
  2992. * return std::make_pair(i, false);
  2993. * }
  2994. *
  2995. * private:
  2996. * char c_;
  2997. * };
  2998. *
  2999. * namespace asio {
  3000. * template <> struct is_match_condition<match_char>
  3001. * : public boost::true_type {};
  3002. * } // namespace asio
  3003. * ...
  3004. * void handler(const boost::system::error_code& e, std::size_t size);
  3005. * ...
  3006. * std::string data;
  3007. * boost::asio::async_read_until(s, data, match_char('a'), handler);
  3008. * @endcode
  3009. *
  3010. * @par Per-Operation Cancellation
  3011. * This asynchronous operation supports cancellation for the following
  3012. * boost::asio::cancellation_type values:
  3013. *
  3014. * @li @c cancellation_type::terminal
  3015. *
  3016. * @li @c cancellation_type::partial
  3017. *
  3018. * if they are also supported by the @c AsyncReadStream type's
  3019. * @c async_read_some operation.
  3020. */
  3021. template <typename AsyncReadStream,
  3022. typename DynamicBuffer_v2, typename MatchCondition,
  3023. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  3024. std::size_t)) ReadToken = default_completion_token_t<
  3025. typename AsyncReadStream::executor_type>>
  3026. auto async_read_until(AsyncReadStream& s,
  3027. DynamicBuffer_v2 buffers, MatchCondition match_condition,
  3028. ReadToken&& token = default_completion_token_t<
  3029. typename AsyncReadStream::executor_type>(),
  3030. constraint_t<
  3031. is_match_condition<MatchCondition>::value
  3032. > = 0,
  3033. constraint_t<
  3034. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  3035. > = 0)
  3036. -> decltype(
  3037. async_initiate<ReadToken,
  3038. void (boost::system::error_code, std::size_t)>(
  3039. declval<detail::initiate_async_read_until_match_v2<AsyncReadStream>>(),
  3040. token, static_cast<DynamicBuffer_v2&&>(buffers), match_condition));
  3041. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  3042. /*@}*/
  3043. } // namespace asio
  3044. } // namespace boost
  3045. #include <boost/asio/detail/pop_options.hpp>
  3046. #include <boost/asio/impl/read_until.hpp>
  3047. #endif // BOOST_ASIO_READ_UNTIL_HPP