Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino String/Stream add features #201

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
22 changes: 17 additions & 5 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ void String::move(String &rhs)
String & String::operator = (const String &rhs)
{
if (this == &rhs) return *this;

if (rhs.buffer) copy(rhs.buffer, rhs.len);
else invalidate();

return *this;
}

Expand All @@ -253,7 +253,7 @@ String & String::operator = (const char *cstr)
{
if (cstr) copy(cstr, strlen(cstr));
else invalidate();

return *this;
}

Expand Down Expand Up @@ -484,7 +484,7 @@ bool String::equalsIgnoreCase( const String &s2 ) const
const char *p2 = s2.buffer;
while (*p1) {
if (tolower(*p1++) != tolower(*p2++)) return false;
}
}
return true;
}

Expand Down Expand Up @@ -515,7 +515,7 @@ char String::charAt(unsigned int loc) const
return operator[](loc);
}

void String::setCharAt(unsigned int loc, char c)
void String::setCharAt(unsigned int loc, char c)
{
if (loc < len) buffer[loc] = c;
}
Expand Down Expand Up @@ -631,6 +631,18 @@ String String::substring(unsigned int left, unsigned int right) const
/* Modification */
/*********************************************/

void String::insert(const String &str, unsigned int index, unsigned int length)
{
if (index > len) return;
length = length < str.len ? length : str.len;
unsigned int size = len + length;
if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
memmove(buffer + index + length, buffer + index, len - index + length);
memcpy(buffer + index, str.buffer, length);
len += length;
return;
}

void String::replace(char find, char replace)
{
if (!buffer) return;
Expand Down
4 changes: 3 additions & 1 deletion api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ class String
String substring( unsigned int beginIndex, unsigned int endIndex ) const;

// modification
void replace(char find, char replace);
void insert(char c, unsigned int index) { return insert(&c, index, 1); };
void insert(const String &str, unsigned int index, unsigned int length);
PiotrekB416 marked this conversation as resolved.
Show resolved Hide resolved
void replace(char find, char replace);
void replace(const String& find, const String& replace);
void remove(unsigned int index);
void remove(unsigned int index, unsigned int count);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ set(TEST_SRCS
src/String/test_indexOf.cpp
src/String/test_lastIndexOf.cpp
src/String/test_length.cpp
src/String/test_insert.cpp
src/String/test_move.cpp
src/String/test_remove.cpp
src/String/test_replace.cpp
Expand Down
47 changes: 47 additions & 0 deletions test/src/String/test_insert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Arduino. All rights reserved.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <catch.hpp>

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/

TEST_CASE ("Testing String::insert(char, index)", "[String-insert-01]")
{
arduino::String str = "ello";
str.insert('h', 0);
REQUIRE(str == String("hello"));
}

TEST_CASE ("Testing String::insert(char, index) with index > length", "[String-insert-02]")
{
arduino::String str = "Hello Arduino";
str.insert('!', str.length() + 1);
REQUIRE(str == String("Hello Arduino"));
}


TEST_CASE ("Testing String::insert(String, index, length) with length >= inserted length", "[String-insert-03]")
{
arduino::String str = "hello ";
str.insert("world", str.length(), 5);
REQUIRE(str == String("hello world"));
}

TEST_CASE ("Testing String::insert(String, index, length) with length < inserted length", "[String-insert-04]")
{
arduino::String str = "Hello ";
str.insert("Arduino 1234", str.length(), 7);
REQUIRE(str == String("Hello Arduino"));
}

Loading