24 #ifndef TVM_RUNTIME_CONTAINER_STRING_H_
25 #define TVM_RUNTIME_CONTAINER_STRING_H_
27 #include <dmlc/endian.h>
28 #include <dmlc/logging.h>
30 #include <tvm/runtime/logging.h>
37 #include <initializer_list>
40 #include <string_view>
41 #include <type_traits>
42 #include <unordered_map>
62 static constexpr
const char*
_type_key =
"runtime.String";
112 String(std::string other);
120 :
String(std::string(other)) {}
164 return memncmp(
data(), other.data(),
size(), other.size());
176 return memncmp(
data(), other,
size(), std::strlen(other));
192 const auto* ptr =
get();
216 char at(
size_t pos)
const {
220 throw std::out_of_range(
"tvm::String index out of bounds");
229 const char*
data()
const {
return get()->data; }
236 operator std::string()
const {
return std::string{
get()->data,
size()}; }
252 const constexpr uint64_t kMultiplier = 1099511628211ULL;
253 const constexpr uint64_t kMod = 2147483647ULL;
258 static_assert(
sizeof(
Union) ==
sizeof(uint64_t),
"sizeof(Union) != sizeof(uint64_t)");
259 const char* it =
data;
260 const char* end = it +
size;
262 for (; it + 8 <= end; it += 8) {
263 if (DMLC_IO_NO_ENDIAN_SWAP) {
282 result = (result * kMultiplier + u.b) % kMod;
306 if (!DMLC_IO_NO_ENDIAN_SWAP) {
307 std::swap(u.a[0], u.a[7]);
308 std::swap(u.a[1], u.a[6]);
309 std::swap(u.a[2], u.a[5]);
310 std::swap(u.a[3], u.a[4]);
312 result = (result * kMultiplier + u.b) % kMod;
330 static int memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count);
342 static String Concat(
const char* lhs,
size_t lhs_size,
const char* rhs,
size_t rhs_size) {
343 std::string
ret(lhs, lhs_size);
344 ret.append(rhs, rhs_size);
369 explicit FromStd(std::string other) : data_container{other} {}
373 std::string data_container;
379 auto ptr = make_object<StringObj::FromStd>(std::move(other));
380 ptr->size = ptr->data_container.size();
381 ptr->data = ptr->data_container.data();
382 data_ = std::move(ptr);
386 String replace{std::move(other)};
387 data_.swap(replace.data_);
394 size_t lhs_size = lhs.
size();
395 size_t rhs_size = rhs.
size();
400 size_t lhs_size = lhs.
size();
401 size_t rhs_size = rhs.size();
406 size_t lhs_size = lhs.
size();
407 size_t rhs_size = rhs.
size();
412 size_t lhs_size = std::strlen(lhs);
413 size_t rhs_size = rhs.
size();
418 size_t lhs_size = lhs.
size();
419 size_t rhs_size = std::strlen(rhs);
490 out.write(input.
data(), input.
size());
494 inline int String::memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count) {
495 if (lhs == rhs && lhs_count == rhs_count)
return 0;
497 for (
size_t i = 0; i < lhs_count && i < rhs_count; ++i) {
498 if (lhs[i] < rhs[i])
return -1;
499 if (lhs[i] > rhs[i])
return 1;
501 if (lhs_count < rhs_count) {
503 }
else if (lhs_count > rhs_count) {
523 return String::memncmp(str_a->data, str_b->data, str_a->size, str_b->size) == 0;
539 std::size_t operator()(const ::tvm::runtime::String& str)
const {
540 return ::tvm::runtime::String::StableHashBytes(str.data(), str.size());
Base class of all object reference.
Definition: object.h:519
const Object * get() const
Definition: object.h:554
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:910
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:530
base class of all object containers.
Definition: object.h:171
An object representing string moved from std::string.
Definition: string.h:359
FromStd(std::string other)
Construct a new FromStd object.
Definition: string.h:369
An object representing string. It's POD type.
Definition: string.h:53
const char * data
The pointer to string data.
Definition: string.h:56
static constexpr const char * _type_key
Definition: string.h:62
uint64_t size
The length of the string object.
Definition: string.h:59
static constexpr const uint32_t _type_index
Definition: string.h:61
TVM_DECLARE_FINAL_OBJECT_INFO(StringObj, Object)
Reference to string objects.
Definition: string.h:98
int compare(const char *other) const
Compares this to other.
Definition: string.h:175
static bool CanConvertFrom(const TVMArgValue &val)
Check if a TVMArgValue can be converted to String, i.e. it can be std::string or String.
Definition: packed_func.h:2683
friend String operator+(const String &lhs, const String &rhs)
Definition: string.h:393
String & operator=(std::string other)
Change the value the reference object points to.
Definition: string.h:385
const char * data() const
Return the data pointer.
Definition: string.h:229
String(const char *other)
Construct a new String object.
Definition: string.h:119
size_t length() const
Return the length of the string.
Definition: string.h:201
static uint64_t StableHashBytes(const char *data, size_t size)
Hash the binary bytes.
Definition: string.h:251
bool empty() const
Retun if the string is empty.
Definition: string.h:208
char at(size_t pos) const
Read an element.
Definition: string.h:216
int compare(const std::string &other) const
Compares this String object to other.
Definition: string.h:163
String()
Construct an empty string.
Definition: string.h:103
const char * c_str() const
Returns a pointer to the char array in the string.
Definition: string.h:184
String(std::nullptr_t)
Construct a new null object.
Definition: string.h:125
int compare(const String &other) const
Compares this String object to other.
Definition: string.h:151
size_t size() const
Return the length of the string.
Definition: string.h:191
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(String, ObjectRef, StringObj)
IntSet Union(const Array< IntSet > &sets)
Create a union set of all sets, possibly relaxed.
bool operator<(const String &lhs, const std::string &rhs)
Definition: string.h:424
String operator+(const String &lhs, const String &rhs)
Definition: string.h:393
Array< T > Concat(Array< T > lhs, const Array< T > &rhs)
Concat two Arrays.
Definition: array.h:899
bool operator!=(const String &lhs, const std::string &rhs)
Definition: string.h:479
bool operator<=(const String &lhs, const std::string &rhs)
Definition: string.h:446
bool operator>=(const String &lhs, const std::string &rhs)
Definition: string.h:457
bool operator==(const String &lhs, const std::string &rhs)
Definition: string.h:468
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
bool operator>(const String &lhs, const std::string &rhs)
Definition: string.h:435
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
A managed object in the TVM runtime.
Base utilities for common POD(plain old data) container types.
Runtime memory management.
String-aware ObjectRef hash functor.
Definition: base.h:50
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Check if the two ObjectRef are equal.
Definition: string.h:517
size_t operator()(const ObjectRef &a) const
Calculate the hash code of an ObjectRef.
Definition: string.h:510
ObjectRef hash functor.
Definition: object.h:655
@ kRuntimeString
runtime::String.
Definition: object.h:66