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 fromall commits
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
8 changes: 8 additions & 0 deletionsapi/Stream.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -255,6 +255,14 @@ String Stream::readStringUntil(char terminator)
return ret;
}

String Stream::readLine()
{
String ret = readStringUntil('\n');
if (ret.endsWith("\r"))
ret.remove(ret.length()-1);
return ret;
}

int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
// any zero length target string automatically matches and would make
// a mess of the rest of the algorithm.
Expand Down
5 changes: 3 additions & 2 deletionsapi/Stream.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,7 +67,7 @@ class Stream : public Print

void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
unsigned long getTimeout(void) { return _timeout; }

bool find(const char *target); // reads data from the stream until the target string is found
bool find(const uint8_t *target) { return find ((const char *)target); }
// returns true if target string is found, false if timed out (see setTimeout)
Expand DownExpand Up@@ -107,6 +107,7 @@ class Stream : public Print
// Arduino String functions to be added here
String readString();
String readStringUntil(char terminator);
String readLine();

protected:
long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
Expand All@@ -130,4 +131,4 @@ class Stream : public Print

}

using arduino::Stream;
using arduino::Stream;
12 changes: 12 additions & 0 deletionsapi/String.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -625,6 +625,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 && !reserve(size)) return; // XXX: tell user!
memmove(buffer + index + length, buffer + index, len - index + 1);
memcpy(buffer + index, str.buffer, length);
len += length;
return;
}

void String::replace(char find, char replace)
{
if (!buffer) return;
Expand Down
2 changes: 2 additions & 0 deletionsapi/String.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -202,6 +202,8 @@ class String
String substring( unsigned int beginIndex, unsigned int endIndex ) const;

// modification
void insert(char c, unsigned int index) { return insert(String(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);
Expand Down
2 changes: 2 additions & 0 deletionstest/CMakeLists.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,7 @@ set(TEST_SRCS
src/Stream/test_parseInt.cpp
src/Stream/test_readBytes.cpp
src/Stream/test_readBytesUntil.cpp
src/Stream/test_readLine.cpp
src/Stream/test_readString.cpp
src/Stream/test_readStringUntil.cpp
src/Stream/test_setTimeout.cpp
Expand All@@ -81,6 +82,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
43 changes: 43 additions & 0 deletionstest/src/Stream/test_readLine.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023 Arduino. All rights reserved.
*/

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

#include <catch.hpp>

#include <MillisFake.h>
#include <StreamMock.h>

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

TEST_CASE ("Testing 'readLine' with no EOL character", "[Stream-readLine-01]")
{
StreamMock mock;
mock.setTimeout(10);
millis_autoOn();
mock << "Hello Arduino";
REQUIRE(mock.readLine() == arduino::String("Hello Arduino"));
}

TEST_CASE ("Testing 'readLine' with Unix EOL character", "[Stream-readLine-02]")
{
StreamMock mock;
mock.setTimeout(10);
millis_autoOn();
mock << "Hello\nArduino\n";
REQUIRE(mock.readLine() == arduino::String("Hello"));
}

TEST_CASE ("Testing 'readLine' with DOS EOL character", "[Stream-readLine-03]")
{
StreamMock mock;
mock.setTimeout(10);
millis_autoOn();
mock << "Hello\r\nArduino\r\n";
REQUIRE(mock.readLine() == arduino::String("Hello"));
}
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 <api/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