Skip to content

Commit d49024c

Browse files
committed
Implement strstr_P, add pgmspace tests (#1749)
1 parent 0a320b9 commit d49024c

File tree

4 files changed

+54
-82
lines changed

4 files changed

+54
-82
lines changed

cores/esp8266/pgmspace.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
*/
1919

2020
#include <ctype.h>
21+
#include <stdint.h>
22+
#include <stddef.h>
23+
#include <stdbool.h>
24+
#include <stdarg.h>
2125
#include "pgmspace.h"
2226

2327
size_t strnlen_P(PGM_P s, size_t size) {
@@ -26,6 +30,33 @@ size_t strnlen_P(PGM_P s, size_t size) {
2630
return (size_t) (cp - s);
2731
}
2832

33+
char* strstr_P(const char* haystack, PGM_P needle)
34+
{
35+
const char* pn = reinterpret_cast<const char*>(needle);
36+
if (haystack[0] == 0) {
37+
if (pgm_read_byte(pn)) {
38+
return NULL;
39+
}
40+
return (char*) haystack;
41+
}
42+
43+
while (*haystack) {
44+
size_t i = 0;
45+
while (true) {
46+
char n = pgm_read_byte(pn + i);
47+
if (n == 0) {
48+
return (char *) haystack;
49+
}
50+
if (n != haystack[i]) {
51+
break;
52+
}
53+
++i;
54+
}
55+
++haystack;
56+
}
57+
return NULL;
58+
}
59+
2960
void* memcpy_P(void* dest, PGM_VOID_P src, size_t count) {
3061
const uint8_t* read = reinterpret_cast<const uint8_t*>(src);
3162
uint8_t* write = reinterpret_cast<uint8_t*>(dest);
@@ -203,7 +234,7 @@ int printf_P(PGM_P formatP, ...) {
203234
char* format = new char[fmtLen + 1];
204235
strcpy_P(format, formatP);
205236

206-
ret = os_printf(format, arglist);
237+
ret = printf(format, arglist);
207238

208239
delete[] format;
209240

@@ -240,7 +271,7 @@ int vsnprintf_P(char* str, size_t strSize, PGM_P formatP, va_list ap) {
240271
char* format = new char[fmtLen + 1];
241272
strcpy_P(format, formatP);
242273

243-
ret = ets_vsnprintf(str, strSize, format, ap);
274+
ret = vsnprintf(str, strSize, format, ap);
244275

245276
delete[] format;
246277

cores/esp8266/pgmspace.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#ifndef __PGMSPACE_H_
22
#define __PGMSPACE_H_
33

4+
#include <stdint.h>
5+
#include <stdio.h>
6+
7+
#ifdef __ets__
8+
49
#ifdef __cplusplus
510
extern "C" {
611
#endif
7-
#include <stdint.h>
8-
#include <stdio.h>
912
#include "ets_sys.h"
1013
#include "osapi.h"
1114
#ifdef __cplusplus
@@ -16,6 +19,13 @@ extern "C" {
1619
#define PGM_P const char *
1720
#define PGM_VOID_P const void *
1821
#define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))
22+
#else //__ets__
23+
#define PROGMEM
24+
#define PGM_P const char *
25+
#define PGM_VOID_P const void *
26+
#define PSTR(s) (s)
27+
28+
#endif // __ets__
1929

2030

2131
#define _SFR_BYTE(n) (n)
@@ -62,6 +72,8 @@ int strncasecmp_P(const char* str1, PGM_P str2P, size_t size);
6272
size_t strnlen_P(PGM_P s, size_t size);
6373
#define strlen_P(strP) strnlen_P((strP), SIZE_IRRELEVANT)
6474

75+
char* strstr_P(const char* haystack, PGM_P needle);
76+
6577
int printf_P(PGM_P formatP, ...) __attribute__((format(printf, 1, 2)));
6678
int sprintf_P(char *str, PGM_P formatP, ...) __attribute__((format(printf, 2, 3)));
6779
int snprintf_P(char *str, size_t strSize, PGM_P formatP, ...) __attribute__((format(printf, 3, 4)));
@@ -74,6 +86,7 @@ int vsnprintf_P(char *str, size_t strSize, PGM_P formatP, va_list ap) __attribut
7486
// b3, b2, b1, b0
7587
// w1, w0
7688

89+
#ifdef __ets__
7790
#define pgm_read_byte(addr) \
7891
(__extension__({ \
7992
PGM_P __local = (PGM_P)(addr); /* isolate varible for macro expansion */ \
@@ -91,6 +104,10 @@ int vsnprintf_P(char *str, size_t strSize, PGM_P formatP, va_list ap) __attribut
91104
uint16_t __result = ((*__addr32) >> (__offset * 8)); \
92105
__result; \
93106
}))
107+
#else //__ets__
108+
#define pgm_read_byte(addr) (*reinterpret_cast<const uint8_t*>(addr))
109+
#define pgm_read_word(addr) (*reinterpret_cast<const uint16_t*>(addr))
110+
#endif //__ets__
94111

95112
#define pgm_read_dword(addr) (*reinterpret_cast<const uint32_t*>(addr))
96113
#define pgm_read_float(addr) (*reinterpret_cast<const float*>(addr))

tests/host/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\
1717
Print.cpp \
1818
FS.cpp \
1919
spiffs_api.cpp \
20+
pgmspace.cpp \
2021
)
2122

2223
CORE_C_FILES := $(addprefix $(CORE_PATH)/,\
@@ -41,6 +42,7 @@ INC_PATHS += $(addprefix -I, \
4142

4243
TEST_CPP_FILES := \
4344
fs/test_fs.cpp \
45+
core/test_pgmspace.cpp \
4446

4547
CXXFLAGS += -std=c++11 -Wall -coverage -O0
4648
CFLAGS += -std=c99 -Wall -coverage -O0

tests/host/common/pgmspace.h

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)