read_until.hpp 125 KB

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