Text utilities (pocketmage_io)
Text utilities (pocketmage_io)
Text utilities API
pocketmage_io is a set of small string helpers for parsing and formatting. They operate on Arduino String and std::vector<String>.
Reference
std::vector<String> splitString(const String& str, char delimiter);
String joinString(const std::vector<String>& parts, char delimiter);
String removeChar(String str, char character);
int stringToInt(const String& str, int defaultVal = -1);
splitStringsplits ondelimiter, keeping empty tokens:"a||b"with'|'becomes{"a", "", "b"}. Use it for command or CSV-style input where empty fields matter.joinStringis the inverse: joinspartswithdelimiterbetween pairs.removeCharstrips every occurrence ofcharacterand returns the new string (a copy; the argument is taken by value).stringToIntparses a decimal integer and returnsdefaultValwhen the string is empty, contains non-digits, or fails to parse. This is the safe integer reader for user input (neveratoion untrusted text).
Example
#include <pocketmage.h>
int parseDuration(String raw) {
// "play 15" -> 15, anything else -> -1
std::vector<String> parts = splitString(raw, ' ');
if (parts.size() != 2) return -1;
return stringToInt(parts[1], -1);
}
C ABI (pm_*)
External ELF apps call these C wrappers rather than the C++ functions above; they are generated from them by gen_app_api.py and declared in pm_sdk_app.h (pulled in via pm_app_api.h). See the function index and the projection model for the general rules.
int pm_io_split_string_count(const char* a0, char a1);
int pm_io_split_string_get(const char* a0, char a1, int index, char* out, size_t out_size);
const char* pm_io_join_string(const char* const* a0_items, int a0_count, char a1);
const char* pm_io_remove_char(const char* a0, char a1);
int pm_io_string_to_int(const char* a0, int a1);
pm_io_split_string_count/pm_io_split_string_getprojectsplitString(), which returnsstd::vector<String>. Callpm_io_split_string_count(a0, a1)to get the token count, thenpm_io_split_string_get(a0, a1, index, out, out_size)per index to copy a token into the caller bufferout(at mostout_sizebytes).a0is the input,a1is the delimiter._getreturns the token length or -1 for a bad index. These write into the caller buffer, not the rotating pool, so an arbitrary-length token is not truncated to 256 bytes.pm_io_join_string(a0_items, a0_count, a1)isjoinString(parts, delimiter); thestd::vector<String>becomes aconst char* const*arraya0_itemsof lengtha0_count, anda1is the delimiter. Returns a pointer into the rotating string pool.pm_io_remove_char(a0, a1)isremoveChar(str, character); strips everya1froma0. Returns a pointer into the rotating string pool.pm_io_string_to_int(a0, a1)isstringToInt(str, defaultVal);a1is the fallback returned whena0is empty or not a decimal integer.