read_until.hpp 129 KB

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