read_until.hpp 129 KB

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