Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Arduino String/Stream add features#201

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
PiotrekB416 wants to merge10 commits intoarduino:master
base:master
Choose a base branch
Loading
fromPiotrekB416:Feat/more-string-functions
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Add String.insert
  • Loading branch information
@PiotrekB416
PiotrekB416 committedSep 1, 2023
commitd5aea6311de36ba9fae8e865a127fab4b8195f96
22 changes: 17 additions & 5 deletionsapi/String.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 deletionapi/String.h
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
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 deletionstest/CMakeLists.txt
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest/src/String/test_insert.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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"));
}


[8]ページ先頭

©2009-2025 Movatter.jp