24 #ifndef TVM_RUNTIME_CONTAINER_STRING_H_ 25 #define TVM_RUNTIME_CONTAINER_STRING_H_ 27 #include <dmlc/logging.h> 29 #include <tvm/runtime/logging.h> 36 #include <initializer_list> 39 #include <unordered_map> 48 #if defined(__cpp_lib_experimental_string_view) && __cpp_lib_experimental_string_view >= 201411 49 #define TVM_USE_CXX14_STRING_VIEW_HASH 1 51 #define TVM_USE_CXX14_STRING_VIEW_HASH 0 56 #if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201606 57 #define TVM_USE_CXX17_STRING_VIEW_HASH 1 59 #define TVM_USE_CXX17_STRING_VIEW_HASH 0 62 #if TVM_USE_CXX17_STRING_VIEW_HASH 63 #include <string_view> 64 #elif TVM_USE_CXX14_STRING_VIEW_HASH 65 #include <experimental/string_view> 68 #include <type_traits> 92 static constexpr
const uint32_t _type_index = TypeIndex::kRuntimeString;
93 static constexpr
const char* _type_key =
"runtime.String";
143 String(std::string other);
165 inline String& operator=(std::string other);
172 inline String& operator=(
const char* other);
183 return memncmp(data(), other.
data(), size(), other.
size());
195 return memncmp(data(), other.data(), size(), other.size());
207 return memncmp(data(), other, size(), std::strlen(other));
215 const char*
c_str()
const {
return get()->data; }
223 const auto* ptr =
get();
239 bool empty()
const {
return size() == 0; }
247 char at(
size_t pos)
const {
251 throw std::out_of_range(
"tvm::String index out of bounds");
260 const char*
data()
const {
return get()->data; }
267 operator std::string()
const {
return std::string{
get()->data, size()}; }
275 inline operator llvm::StringRef()
const;
282 inline static bool CanConvertFrom(
const TVMArgValue& val);
290 static size_t HashBytes(
const char* data,
size_t size) {
293 #if TVM_USE_CXX17_STRING_VIEW_HASH 294 return std::hash<std::string_view>()(std::string_view(data, size));
295 #elif TVM_USE_CXX14_STRING_VIEW_HASH 296 return std::hash<std::experimental::string_view>()(std::experimental::string_view(data, size));
298 return std::hash<std::string>()(std::string(data, size));
315 static int memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count);
327 static String Concat(
const char* lhs,
size_t lhs_size,
const char* rhs,
size_t rhs_size) {
328 std::string
ret(lhs, lhs_size);
329 ret.append(rhs, rhs_size);
354 explicit FromStd(std::string other) : data_container{other} {}
358 std::string data_container;
363 inline String::String(std::string other) {
364 auto ptr = make_object<StringObj::FromStd>(std::move(other));
365 ptr->
size = ptr->data_container.size();
366 ptr->data = ptr->data_container.data();
367 data_ = std::move(ptr);
370 inline String& String::operator=(std::string other) {
371 String replace{std::move(other)};
372 data_.swap(replace.data_);
376 inline String& String::operator=(
const char* other) {
return operator=(std::string(other)); }
379 size_t lhs_size = lhs.
size();
380 size_t rhs_size = rhs.
size();
385 size_t lhs_size = lhs.
size();
386 size_t rhs_size = rhs.size();
391 size_t lhs_size = lhs.
size();
392 size_t rhs_size = rhs.
size();
397 size_t lhs_size = std::strlen(lhs);
398 size_t rhs_size = rhs.
size();
403 size_t lhs_size = lhs.
size();
404 size_t rhs_size = std::strlen(rhs);
475 out.write(input.
data(), input.
size());
479 inline int String::memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count) {
480 if (lhs == rhs && lhs_count == rhs_count)
return 0;
482 for (
size_t i = 0; i < lhs_count && i < rhs_count; ++i) {
483 if (lhs[i] < rhs[i])
return -1;
484 if (lhs[i] > rhs[i])
return 1;
486 if (lhs_count < rhs_count) {
488 }
else if (lhs_count > rhs_count) {
495 inline size_t ObjectHash::operator()(
const ObjectRef& a)
const {
497 return String::HashBytes(str->data, str->size);
508 return String::memncmp(str_a->data, str_b->data, str_a->size, str_b->size) == 0;
524 std::size_t operator()(const ::tvm::runtime::String& str)
const {
525 return ::tvm::runtime::String::HashBytes(str.data(), str.size());
530 #endif // TVM_RUNTIME_CONTAINER_STRING_H_ bool operator>(const char *lhs, const String &rhs)
Definition: string.h:428
uint64_t size
The length of the string object.
Definition: string.h:90
String-aware ObjectRef hash functor.
Definition: base.h:50
int compare(const char *other) const
Compares this to other.
Definition: string.h:206
String(std::nullptr_t)
Construct a new null object.
Definition: string.h:156
char at(size_t pos) const
Read an element.
Definition: string.h:247
const char * data() const
Return the data pointer.
Definition: string.h:260
bool operator!=(const char *lhs, const String &rhs)
Definition: string.h:472
bool operator<(const char *lhs, const String &rhs)
Definition: string.h:417
An object representing string moved from std::string.
Definition: string.h:344
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
bool operator>=(const char *lhs, const String &rhs)
Definition: string.h:450
String()
Construct an empty string.
Definition: string.h:134
Runtime memory management.
Definition: loop_state.h:456
static size_t HashBytes(const char *data, size_t size)
Hash the binary bytes.
Definition: string.h:290
Base utilities for common POD(plain old data) container types.
std::ostream & operator<<(std::ostream &out, const String &input)
Definition: string.h:474
base class of all object containers.
Definition: object.h:165
size_t length() const
Return the length of the string.
Definition: string.h:232
const char * c_str() const
Returns a pointer to the char array in the string.
Definition: string.h:215
const char * data
The pointer to string data.
Definition: string.h:87
String operator+(const String &lhs, const char *rhs)
Definition: string.h:402
FromStd(std::string other)
Construct a new FromStd object.
Definition: string.h:354
ObjectRef hash functor.
Definition: object.h:617
bool operator<=(const char *lhs, const String &rhs)
Definition: string.h:439
size_t size() const
Return the length of the string.
Definition: string.h:222
Reference to string objects.
Definition: string.h:129
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:515
Base class of all object reference.
Definition: object.h:504
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:664
bool operator==(const char *lhs, const String &rhs)
Definition: string.h:461
An object representing string. It's POD type.
Definition: string.h:84
String(const char *other)
Construct a new String object.
Definition: string.h:150
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:583
bool empty() const
Retun if the string is empty.
Definition: string.h:239
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:858
int compare(const String &other) const
Compares this String object to other.
Definition: string.h:182
const Op & ret()
Return value.
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:721
Array< T > Concat(Array< T > lhs, const Array< T > &rhs)
Concat two Arrays.
Definition: array.h:719
int compare(const std::string &other) const
Compares this String object to other.
Definition: string.h:194