@@ -24,7 +24,10 @@ namespace rpc_impl_ {
2424
2525constexpr int32_t MAX_TIMEOUT_S = 86400 ;
2626
27- // TODO: change uint64_t to string::size_type after moving it from uint32_t to uint64_t
27+ constexpr auto SMALL_STRING_SIZE_LEN = 1 ;
28+ constexpr auto MEIDUM_STRING_SIZE_LEN = 3 ;
29+ constexpr auto LARGE_STRING_SIZE_LEN = 7 ;
30+
2831constexpr uint64_t SMALL_STRING_MAX_LEN = 253 ;
2932constexpr uint64_t MEDIUM_STRING_MAX_LEN = (static_cast <uint64_t >(1 ) << 24 ) - 1 ;
3033[[maybe_unused]] constexpr uint64_t LARGE_STRING_MAX_LEN = (static_cast <uint64_t >(1 ) << 56 ) - 1 ;
@@ -324,30 +327,25 @@ bool f$store_double(double v) noexcept {
324327 return true ;
325328}
326329
327- bool f$store_string(const string &v) noexcept { // TODO: support large strings
330+ bool f$store_string(const string &v) noexcept {
328331 auto &rpc_buf{RpcComponentContext::get ().rpc_buffer };
329332
330- string::size_type string_len{v. size () };
331- string::size_type size_len{ };
333+ uint8_t size_len{ };
334+ uint64_t string_len{v. size () };
332335 if (string_len <= rpc_impl_::SMALL_STRING_MAX_LEN) {
333- size_len = 1 ;
336+ size_len = rpc_impl_::SMALL_STRING_SIZE_LEN ;
334337 rpc_buf.store_trivial (static_cast <uint8_t >(string_len));
335338 } else if (string_len <= rpc_impl_::MEDIUM_STRING_MAX_LEN) {
336- size_len = 4 ;
339+ size_len = rpc_impl_::MEIDUM_STRING_SIZE_LEN + 1 ;
337340 rpc_buf.store_trivial (static_cast <uint8_t >(rpc_impl_::MEDIUM_STRING_MAGIC));
338341 rpc_buf.store_trivial (static_cast <uint8_t >(string_len & 0xff ));
339342 rpc_buf.store_trivial (static_cast <uint8_t >((string_len >> 8 ) & 0xff ));
340343 rpc_buf.store_trivial (static_cast <uint8_t >((string_len >> 16 ) & 0xff ));
341344 } else {
342- size_len = 8 ;
343- rpc_buf.store_trivial (static_cast <uint8_t >(rpc_impl_::LARGE_STRING_MAGIC));
344- rpc_buf.store_trivial (static_cast <uint8_t >(string_len & 0xff ));
345- rpc_buf.store_trivial (static_cast <uint8_t >((string_len >> 8 ) & 0xff ));
346- rpc_buf.store_trivial (static_cast <uint8_t >((string_len >> 16 ) & 0xff ));
347- rpc_buf.store_trivial (static_cast <uint8_t >((string_len >> 24 ) & 0xff ));
348- // rpc_buf.store_trivial(static_cast<uint8_t>((string_len >> 32) & 0xff));
349- // rpc_buf.store_trivial(static_cast<uint8_t>((string_len >> 40) & 0xff));
350- // rpc_buf.store_trivial(static_cast<uint8_t>((string_len >> 48) & 0xff));
345+ php_warning (" large strings aren't supported" );
346+ size_len = rpc_impl_::SMALL_STRING_SIZE_LEN;
347+ string_len = 0 ;
348+ rpc_buf.store_trivial (static_cast <uint8_t >(string_len));
351349 }
352350 rpc_buf.store (v.c_str (), string_len);
353351
@@ -388,14 +386,14 @@ string f$fetch_string() noexcept {
388386 return {}; // TODO: error handling
389387 }
390388
391- string::size_type string_len {};
392- string::size_type size_len {};
389+ uint8_t size_len {};
390+ uint64_t string_len {};
393391 switch (first_byte) {
394- case rpc_impl_::LARGE_STRING_MAGIC: { // next 7 bytes are string's length // TODO: support large strings
395- // static_assert(sizeof(string::size_type) >= 8, "string's length doesn't fit platform size");
396- if (rpc_buf.remaining () < 7 ) {
392+ case rpc_impl_::LARGE_STRING_MAGIC: {
393+ if (rpc_buf.remaining () < rpc_impl_::LARGE_STRING_SIZE_LEN) {
397394 return {}; // TODO: error handling
398395 }
396+ size_len = rpc_impl_::LARGE_STRING_SIZE_LEN + 1 ;
399397 const auto first{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ())};
400398 const auto second{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ()) << 8 };
401399 const auto third{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ()) << 16 };
@@ -404,35 +402,40 @@ string f$fetch_string() noexcept {
404402 const auto sixth{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ()) << 40 };
405403 const auto seventh{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ()) << 48 };
406404 string_len = first | second | third | fourth | fifth | sixth | seventh;
407- if (string_len < (1 << 24 )) {
408- php_warning (" long string's length is less than 1 << 24" );
409- }
410- size_len = 8 ;
405+
406+ const auto total_len_with_padding{(size_len + string_len + 3 ) & ~static_cast <uint64_t >(3 )};
407+ rpc_buf.adjust (total_len_with_padding - size_len);
408+ php_warning (" large strings aren't supported" );
409+ return {};
411410 }
412- case rpc_impl_::MEDIUM_STRING_MAGIC: { // next 3 bytes are string's length
413- if (rpc_buf.remaining () < 3 ) {
411+ case rpc_impl_::MEDIUM_STRING_MAGIC: {
412+ if (rpc_buf.remaining () < rpc_impl_::MEIDUM_STRING_SIZE_LEN ) {
414413 return {}; // TODO: error handling
415414 }
415+ size_len = rpc_impl_::MEIDUM_STRING_SIZE_LEN + 1 ;
416416 const auto first{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ())};
417417 const auto second{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ()) << 8 };
418418 const auto third{static_cast <uint64_t >(rpc_buf.fetch_trivial <uint8_t >().value ()) << 16 };
419419 string_len = first | second | third;
420- if (string_len <= 253 ) {
420+
421+ if (string_len <= rpc_impl_::SMALL_STRING_MAX_LEN) {
421422 php_warning (" long string's length is less than 254" );
422423 }
423- size_len = 4 ;
424+ break ;
425+ }
426+ default : {
427+ size_len = rpc_impl_::SMALL_STRING_SIZE_LEN;
428+ string_len = static_cast <uint64_t >(first_byte);
429+ break ;
424430 }
425- default :
426- string_len = static_cast <string::size_type>(first_byte);
427- size_len = 1 ;
428431 }
429432
430- const auto total_len_with_padding{(size_len + string_len + 3 ) & ~static_cast <string::size_type >(3 )};
433+ const auto total_len_with_padding{(size_len + string_len + 3 ) & ~static_cast <uint64_t >(3 )};
431434 if (rpc_buf.remaining () < total_len_with_padding - size_len) {
432435 return {}; // TODO: error handling
433436 }
434437
435- string res{rpc_buf.data () + rpc_buf.pos (), string_len};
438+ string res{rpc_buf.data () + rpc_buf.pos (), static_cast <string::size_type>( string_len) };
436439 rpc_buf.adjust (total_len_with_padding - size_len);
437440 return res;
438441}
0 commit comments