1

add stringview.cc

This commit is contained in:
2022-08-01 20:43:38 +02:00
parent f50ed64c18
commit 9ce177e13e
2 changed files with 16 additions and 26 deletions

View File

@ -0,0 +1,12 @@
#include "user/lib/StringView.h"
bse::string_view bse::string_view::substring(std::size_t first, std::size_t last) const {
if (first < 0 || first > len || last <= first || last > len) {
return nullptr;
}
string_view new_view;
new_view.len = last - first;
new_view.buf = &buf[first];
return new_view;
}

View File

@ -22,39 +22,17 @@ namespace bse {
string_view(const char* str) : len(strlen(str)), buf(str) {}
string_view(const string& str) : len(str.size()), buf(static_cast<char*>(str)) {}
// iterator begin() { return iterator(buf); }
iterator begin() const { return iterator(buf); }
// iterator end() { return iterator(&buf[len]); }
iterator end() const { return iterator(&buf[len]); }
// explicit operator const char*() { return buf; }
explicit operator const char*() const { return buf; }
// char operator[](std::size_t pos) { return buf[pos]; }
char operator[](std::size_t pos) const { return buf[pos]; }
bool operator==(const string_view& other) const { return buf == other.buf; }
bool operator!=(const string_view& other) const { return buf != other.buf; }
bool operator==(const string_view& other) const {
return buf == other.buf;
}
std::size_t size() const { return len; }
bool operator!=(const string_view& other) const {
return buf != other.buf;
}
string_view substring(std::size_t first, std::size_t last) const {
if (first < 0 || first > len || last <= first || last > len) {
return nullptr;
}
string_view new_view;
new_view.len = last - first;
new_view.buf = &buf[first];
return new_view;
}
std::size_t size() const {
return len;
}
string_view substring(std::size_t first, std::size_t last) const;
};
} // namespace bse