move some methods to header file

This commit is contained in:
blueloveTH 2024-03-02 04:06:29 +08:00
parent 2055f9cd46
commit 5e01164dac
2 changed files with 14 additions and 44 deletions

View File

@ -60,11 +60,12 @@ struct Str{
friend std::ostream& operator<<(std::ostream& os, const Str& str); friend std::ostream& operator<<(std::ostream& os, const Str& str);
const char* c_str() const { return data; }
std::string_view sv() const { return std::string_view(data, size); }
std::string str() const { return std::string(data, size); }
Str substr(int start, int len) const; Str substr(int start, int len) const;
Str substr(int start) const; Str substr(int start) const;
const char* c_str() const;
std::string_view sv() const;
std::string str() const;
Str strip(bool left, bool right, const Str& chars) const; Str strip(bool left, bool right, const Str& chars) const;
Str strip(bool left=true, bool right=true) const; Str strip(bool left=true, bool right=true) const;
Str lstrip() const { return strip(true, false); } Str lstrip() const { return strip(true, false); }
@ -90,15 +91,17 @@ struct Str{
struct StrName { struct StrName {
uint16_t index; uint16_t index;
StrName();
explicit StrName(uint16_t index);
StrName(const char* s);
StrName(const Str& s);
std::string_view sv() const;
const char* c_str() const;
bool empty() const { return index == 0; }
Str escape() const; StrName(): index(0) {}
explicit StrName(uint16_t index): index(index) {}
StrName(const char* s): index(get(s).index) {}
StrName(const Str& s): index(get(s.sv()).index) {}
std::string_view sv() const { return _r_interned()[index];}
const char* c_str() const { return _r_interned()[index].c_str(); }
bool empty() const { return index == 0; }
Str escape() const { return Str(sv()).escape(); }
bool operator==(const StrName& other) const noexcept { bool operator==(const StrName& other) const noexcept {
return this->index == other.index; return this->index == other.index;

View File

@ -182,18 +182,6 @@ int utf8len(unsigned char c, bool suppress){
return substr(start, size - start); return substr(start, size - start);
} }
const char* Str::c_str() const{
return data;
}
std::string_view Str::sv() const {
return std::string_view(data, size);
}
std::string Str::str() const {
return std::string(data, size);
}
Str Str::strip(bool left, bool right, const Str& chars) const { Str Str::strip(bool left, bool right, const Str& chars) const {
int L = 0; int L = 0;
int R = u8_length(); int R = u8_length();
@ -414,31 +402,10 @@ int utf8len(unsigned char c, bool suppress){
return StrName(index); return StrName(index);
} }
Str StrName::escape() const {
return Str(sv()).escape();
}
bool StrName::is_valid(int index) { bool StrName::is_valid(int index) {
return _r_interned().find(index) != _r_interned().end(); return _r_interned().find(index) != _r_interned().end();
} }
StrName::StrName(): index(0) {}
StrName::StrName(uint16_t index): index(index) {}
StrName::StrName(const char* s): index(get(s).index) {}
StrName::StrName(const Str& s){
index = get(s.sv()).index;
}
std::string_view StrName::sv() const {
const std::string& str = _r_interned()[index];
return std::string_view(str);
}
const char* StrName::c_str() const{
const std::string& str = _r_interned()[index];
return str.c_str();
}
Str SStream::str(){ Str SStream::str(){
// after this call, the buffer is no longer valid // after this call, the buffer is no longer valid
buffer.reserve(buffer.size() + 1); // allocate one more byte for '\0' buffer.reserve(buffer.size() + 1); // allocate one more byte for '\0'