#include "string.h" #include #include static inline void smemcpy(char * dp, const char * sp, char s) { for(char i=0; i 0 && cstr) { if (n >= cstr[0]) { free(cstr); cstr = nullptr; } else { char i = 1; n++; while (cstr[n]) cstr[i++] = cstr[n++]; cstr[i] = 0; cstr[0] = i - 1; } } return *this; } string & string::operator>>=(char n) { if (n > 0 && cstr) { if (n >= cstr[0]) { free(cstr); cstr = nullptr; } else { cstr[0] -= n; cstr[n + 1] = 0; } } return *this; } string string::operator<<(char n) const { if (n > 0 && cstr) { if (n >= cstr[0]) return string(); else { } } else return *this; } string string::operator>>(char n) const { } static int scmp(const char * s1, const char * s2) { char n = 0; while (s1[n]) { if (s1[n] != s2[n]) return s1[n] - s2[n]; n++; } return 0; } inline bool string::operator==(const string & s) const { if (cstr && s.cstr) return scmp(cstr + 1, s.cstr + 1) == 0; else return !cstr && !s.cstr; } inline bool string::operator==(const char * s) const { if (cstr) return scmp(cstr + 1, s) == 0; else return s[0] == 0; } inline bool string::operator!=(const string & s) const { if (cstr && s.cstr) return scmp(cstr + 1, s.cstr + 1) != 0; else return cstr || s.cstr; } inline bool string::operator!=(const char * s) const { if (cstr) return scmp(cstr + 1, s) == 0; else return s[0] != 0; } inline bool string::operator<(const string & s) const { if (cstr && s.cstr) return scmp(cstr + 1, s.cstr + 1) < 0; else return s.cstr; } inline bool string::operator<(const char * s) const { if (cstr) return scmp(cstr + 1, s) < 0; else return s[0] != 0; } inline bool string::operator<=(const string & s) const { if (cstr && s.cstr) return scmp(cstr + 1, s.cstr + 1) <= 0; else return !cstr || s.cstr; } inline bool string::operator<=(const char * s) const { if (cstr) return scmp(cstr + 1, s) <= 0; else return true; } inline bool string::operator>(const string & s) const { if (cstr && s.cstr) return scmp(cstr + 1, s.cstr + 1) > 0; else return cstr; } inline bool string::operator>(const char * s) const { if (cstr) return scmp(cstr + 1, s) > 0; else return false; } inline bool string::operator>=(const string & s) const { if (cstr && s.cstr) return scmp(cstr + 1, s.cstr + 1) >= 0; else return cstr || !s.cstr; } inline bool string::operator>=(const char * s) const { if (cstr) return scmp(cstr + 1, s) >= 0; else return s[0] == 0; } inline char & string::operator[](char t) { return cstr[t + 1]; } inline char string::operator[](char t) const { return cstr[t + 1]; } string string::substr(char pos, char len) const { if (!cstr || len == 0 || pos >= cstr[0]) return string; else { char l = cstr[0]; if (pos + len > l) len = l - pos; char * p = malloc(len + 2); memcpy(p + 1, cstr + 1 + pos, len); return string(len, p); } } inline int string::find(const string & s) const { return find(s, 0); } inline int string::find(const char * s) const { return find(s, 0); } inline int string::find(char c) const { return find(c, 0); } int string::find(const string & s, char pos) const { if (!s.cstr) return pos; if (cstr) { char l = cstr[0]; char sl = s.cstr[0]; if (sl <= l) { l -= sl; while (pos <= l) { char i = 1; while (s.cstr[i] && s.cstr[i] == cstr[pos + i]) i++; if (!s.cstr[i]) return pos; pos++; } } } return -1; } int string::find(const char * s, char pos) const { if (cstr) { char l = cstr[0]; char sl = sstrlen(s); if (sl <= l) { l -= sl; while (pos <= l) { char i = 0; while (s[i] && s[i] == cstr[pos + i + 1]) i++; if (!s[i]) return pos; pos++; } } } return -1; } int string::find(char c, char pos) const { if (cstr) { char l = cstr[0]; while (pos < l) { if (cstr[pos + 1] == c) return pos; pos++; } } return -1; }