diff --git a/docs/reference.xml b/docs/reference.xml index b8a2affc..c8498bcf 100644 --- a/docs/reference.xml +++ b/docs/reference.xml @@ -430,6 +430,43 @@ + + Returns the position of a substring into another + + + + + + + + Character indexes and occurence number begin at 0. + + + + + + + + + + + + + + + + + + + + + + + Copies a substring out of the original. diff --git a/source/src/command.cpp b/source/src/command.cpp index 1d87f7ab..1e9f8a54 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -1279,11 +1279,35 @@ void substr_(char *fs, char *pa, char *len) result(newstring(fs + ia, ilen)); } +void strpos_(char *haystack, char *needle, char *occurence) +{ + int position = -1; + char *ptr = haystack; + + if(haystack && needle) + for(int iocc = atoi(occurence); iocc >= 0; iocc--) + { + ptr = strstr(ptr, needle); + if (ptr) + { + position = ptr-haystack; + ptr += strlen(needle); + } + else + { + position = -1; + break; + } + } + intret(position); +} + void l0(int p, int v) { string f; string r; formatstring(f)("%%0%dd", p); formatstring(r)(f, v); result(r); } COMMANDN(millis, millis_, ARG_IVAL); COMMANDN(strlen, strlen_, ARG_1STR); COMMANDN(substr, substr_, ARG_3STR); +COMMANDN(strpos, strpos_, ARG_3STR); COMMAND(l0, ARG_2INT); COMMAND(systime, ARG_NONE); COMMANDN(timestamp, timestamp_, ARG_NONE);