Skip to content

Commit

Permalink
use string_view in ExtractString and PutString
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 12, 2025
1 parent 915429b commit efd8e6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
18 changes: 9 additions & 9 deletions libi2pd_client/I2CP.cpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -555,20 +555,20 @@ namespace client
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];
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 ();
if (l + 1 >= len) l = len - 1;
if (l > 255) l = 255; // 1 byte max
buf[0] = l;
memcpy (buf + 1, str.c_str (), l);
memcpy (buf + 1, str.data (), l);
return l + 1;
}

Expand All @@ -578,7 +578,7 @@ namespace client
size_t offset = 0;
while (offset < len)
{
std::string param = ExtractString (buf + offset, len - offset);
auto param = ExtractString (buf + offset, len - offset);
offset += param.length () + 1;
if (buf[offset] != '=')
{
Expand All @@ -587,15 +587,15 @@ namespace client
}
offset++;

std::string value = ExtractString (buf + offset, len - offset);
auto value = ExtractString (buf + offset, len - offset);
offset += value.length () + 1;
if (buf[offset] != ';')
{
LogPrint (eLogWarning, "I2CP: Unexpected character ", buf[offset], " instead ';' after ", value);
break;
}
offset++;
mapping.insert (std::make_pair (param, value));
mapping.emplace (param, value);
}
}

Expand Down Expand Up @@ -921,7 +921,7 @@ namespace client
case 1: // address
{
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 ())
{
// TODO: handle blinded addresses
Expand Down
7 changes: 4 additions & 3 deletions libi2pd_client/I2CP.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -11,6 +11,7 @@

#include <inttypes.h>
#include <string>
#include <string_view>
#include <memory>
#include <mutex>
#include <thread>
Expand Down Expand Up @@ -191,8 +192,8 @@ namespace client

void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);

std::string ExtractString (const uint8_t * buf, size_t len);
size_t PutString (uint8_t * buf, size_t len, const std::string& str);
std::string_view ExtractString (const uint8_t * buf, size_t len);
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 SendSessionStatusMessage (I2CPSessionStatus status);
void SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity);
Expand Down

0 comments on commit efd8e6e

Please sign in to comment.