Last active
December 24, 2015 20:39
-
-
Save Amanieu/6859496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> class BasicStringRef { | |
BasicStringRef(const std::basic_string<T>& other) | |
{ | |
ptr = other.c_str(); | |
len = other.size(); | |
} | |
BasicStringRef(const T* other) | |
{ | |
ptr = other; | |
len = std::char_traits<T>::length(other); | |
} | |
BasicStringRef& operator=(const std::basic_string<T>& other) | |
{ | |
ptr = other.c_str(); | |
len = other.size(); | |
return *this; | |
} | |
BasicStringRef& operator=(const T* other) | |
{ | |
ptr = other; | |
len = std::char_traits<T>::length(other); | |
return *this; | |
} | |
const T& operator[](size_t pos) const | |
{ | |
return ptr[pos]; | |
} | |
const T* begin() const | |
{ | |
return ptr; | |
} | |
const T* end() const | |
{ | |
return ptr + len; | |
} | |
const T* data() const | |
{ | |
return ptr; | |
} | |
const T* c_str() const | |
{ | |
return ptr; | |
} | |
std::basic_string<T> str() const | |
{ | |
return std::basic_string<T>(ptr, len); | |
} | |
bool empty() const | |
{ | |
return len == 0; | |
} | |
size_t size() const | |
{ | |
return len; | |
} | |
operator std::basic_string<T>() const | |
{ | |
return str(); | |
} | |
operator const T*() const | |
{ | |
return ptr; | |
} | |
private: | |
const T* ptr; | |
size_t len; | |
}; | |
typedef BasicStringRef<char> StringRef; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment