string bmlwriter::unescape(cstring val) { if (!val.startswith("-")) return val; string out; for (size_t i=1;i<val.length();i++) { if (val[i]=='-') { byte tmp; if (val[i+1]=='-') { i++; out += '-'; } else if (fromstringhex(val.substr(i+1, i+3), tmp)) { i += 2; out += tmp; } } else out += val[i]; } return out; }
string bmlwriter::escape(cstring val) { string esc = "-"; bool needescape = (val.startswith("-")); for (byte c : val.bytes()) { if (isalnum(c) || c=='.') esc+=c; else if (c=='-') esc+="--"; else { esc+="-"+tostringhex<2>((uint8_t)c); needescape=true; } } if (needescape) return esc; else return val; }
//text must NOT start with & static bool entity_match(const char * ent, cstring text) { size_t entlen = strlen(ent); if (!text.startswith(ent)) return false; return (text[entlen]==';' || ent[entlen+1]=='?'); }