Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 18 additions & 8 deletions api/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal)
}
}

// strlen() for uint8_t*
size_t strlen_uint8(const uint8_t* str) {
if (str == nullptr) return 0; // return 0 for null pointer
const uint8_t* p = str;
while (*p != 0x00) {
p++;
}
return static_cast<size_t>(p - str);
}

// Public Methods
//////////////////////////////////////////////////////////////

Expand All @@ -92,28 +102,28 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi
}

// find returns true if the target string is found
bool Stream::find(const char *target)
bool Stream::find(const uint8_t *target)
{
return findUntil(target, strlen(target), NULL, 0);
return findUntil(target, strlen_uint8(target), NULL, 0);
}

// reads data from the stream until the target string of given length is found
// returns true if target string is found, false if timed out
bool Stream::find(const char *target, size_t length)
bool Stream::find(const uint8_t *target, size_t length)
{
return findUntil(target, length, NULL, 0);
}

// as find but search ends if the terminator string is found
bool Stream::findUntil(const char *target, const char *terminator)
bool Stream::findUntil(const uint8_t *target, const uint8_t *terminator)
{
return findUntil(target, strlen(target), terminator, strlen(terminator));
return findUntil(target, strlen_uint8(target), terminator, strlen_uint8(terminator));
}

// reads data from the stream until the target string of the given length is found
// search terminated if the terminator string is found
// returns true if target string is found, false if terminated or timed out
bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
bool Stream::findUntil(const uint8_t *target, size_t targetLen, const uint8_t *terminator, size_t termLen)
{
if (terminator == NULL) {
MultiTarget t[1] = {{target, targetLen, 0}};
Expand Down Expand Up @@ -270,7 +280,7 @@ int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {

for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
// the simple case is if we match, deal with that first.
if ((char)c == t->str[t->index]) {
if (c == t->str[t->index]) {
if (++t->index == t->len)
return t - targets;
else
Expand All @@ -288,7 +298,7 @@ int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
do {
--t->index;
// first check if current char works against the new current index
if ((char)c != t->str[t->index])
if (c != t->str[t->index])
continue;

// if it's the only char then we're good, nothing more to check
Expand Down
23 changes: 12 additions & 11 deletions api/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include <inttypes.h>

#include "Print.h"

// compatibility macros for testing
Expand Down Expand Up @@ -67,22 +68,22 @@ 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); }

bool find(const uint8_t *target); // reads data from the stream until the target string is found
bool find(const char *target) { return find ((const uint8_t *)target); }
// returns true if target string is found, false if timed out (see setTimeout)

bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found
bool find(const uint8_t *target, size_t length) { return find ((const char *)target, length); }
bool find(const uint8_t *target, size_t length); // reads data from the stream until the target string of given length is found
bool find(const char *target, size_t length) { return find ((const uint8_t *)target, length); }
// returns true if target string is found, false if timed out

bool find(char target) { return find (&target, 1); }
bool find(uint8_t target) { return find (&target, 1); }

bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found
bool findUntil(const uint8_t *target, const char *terminator) { return findUntil((const char *)target, terminator); }
bool findUntil(const uint8_t *target, const uint8_t *terminator); // as find but search ends if the terminator string is found
bool findUntil(const char *target, const char *terminator) { return findUntil((const uint8_t *)target, (const uint8_t *)terminator); }

bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found
bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {return findUntil((const char *)target, targetLen, terminate, termLen); }
bool findUntil(const uint8_t *target, size_t targetLen, const uint8_t *terminator, size_t termLen);
bool findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen) { return findUntil((const uint8_t*)target, targetLen, (const uint8_t*)terminator, termLen); }

long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
// returns the first valid (long) integer value from the current position.
Expand Down Expand Up @@ -116,7 +117,7 @@ class Stream : public Print
// the public API simple, these overload remains protected.

struct MultiTarget {
const char *str; // string you're searching for
const uint8_t *str; // string you're searching for
size_t len; // length of string you're searching for
size_t index; // index used by the search routine.
};
Expand Down
Loading