1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-15 22:09:57 +00:00

use string_view in ExtractString and PutString

This commit is contained in:
orignal 2025-01-11 22:34:18 -05:00
parent 915429bb49
commit efd8e6e65b
2 changed files with 13 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2024, The PurpleI2P Project * Copyright (c) 2013-2025, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -555,20 +555,20 @@ namespace client
m_IsSending = false; m_IsSending = false;
} }
std::string I2CPSession::ExtractString (const uint8_t * buf, size_t len) std::string_view I2CPSession::ExtractString (const uint8_t * buf, size_t len)
{ {
uint8_t l = buf[0]; uint8_t l = buf[0];
if (l > len) l = len; if (l > len) l = len;
return std::string ((const char *)(buf + 1), l); return { (const char *)(buf + 1), l };
} }
size_t I2CPSession::PutString (uint8_t * buf, size_t len, const std::string& str) size_t I2CPSession::PutString (uint8_t * buf, size_t len, std::string_view str)
{ {
auto l = str.length (); auto l = str.length ();
if (l + 1 >= len) l = len - 1; if (l + 1 >= len) l = len - 1;
if (l > 255) l = 255; // 1 byte max if (l > 255) l = 255; // 1 byte max
buf[0] = l; buf[0] = l;
memcpy (buf + 1, str.c_str (), l); memcpy (buf + 1, str.data (), l);
return l + 1; return l + 1;
} }
@ -578,7 +578,7 @@ namespace client
size_t offset = 0; size_t offset = 0;
while (offset < len) while (offset < len)
{ {
std::string param = ExtractString (buf + offset, len - offset); auto param = ExtractString (buf + offset, len - offset);
offset += param.length () + 1; offset += param.length () + 1;
if (buf[offset] != '=') if (buf[offset] != '=')
{ {
@ -587,7 +587,7 @@ namespace client
} }
offset++; offset++;
std::string value = ExtractString (buf + offset, len - offset); auto value = ExtractString (buf + offset, len - offset);
offset += value.length () + 1; offset += value.length () + 1;
if (buf[offset] != ';') if (buf[offset] != ';')
{ {
@ -595,7 +595,7 @@ namespace client
break; break;
} }
offset++; offset++;
mapping.insert (std::make_pair (param, value)); mapping.emplace (param, value);
} }
} }
@ -921,7 +921,7 @@ namespace client
case 1: // address case 1: // address
{ {
auto name = ExtractString (buf + 11, len - 11); auto name = ExtractString (buf + 11, len - 11);
auto addr = i2p::client::context.GetAddressBook ().GetAddress (name); auto addr = i2p::client::context.GetAddressBook ().GetAddress (std::string (name)); // TODO: GetAddress should take string_view
if (!addr || !addr->IsIdentHash ()) if (!addr || !addr->IsIdentHash ())
{ {
// TODO: handle blinded addresses // TODO: handle blinded addresses

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2024, The PurpleI2P Project * Copyright (c) 2013-2025, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -11,6 +11,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <string> #include <string>
#include <string_view>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
@ -191,8 +192,8 @@ namespace client
void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
std::string ExtractString (const uint8_t * buf, size_t len); std::string_view ExtractString (const uint8_t * buf, size_t len);
size_t PutString (uint8_t * buf, size_t len, const std::string& str); size_t PutString (uint8_t * buf, size_t len, std::string_view str);
void ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping); void ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping);
void SendSessionStatusMessage (I2CPSessionStatus status); void SendSessionStatusMessage (I2CPSessionStatus status);
void SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity); void SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity);