24 #ifndef TVM_RUNTIME_CONTAINER_STRING_H_
25 #define TVM_RUNTIME_CONTAINER_STRING_H_
27 #include <dmlc/endian.h>
29 #include <tvm/runtime/logging.h>
36 #include <initializer_list>
39 #include <string_view>
40 #include <type_traits>
41 #include <unordered_map>
61 static constexpr
const char*
_type_key =
"runtime.String";
111 String(std::string other);
119 :
String(std::string(other)) {}
163 return memncmp(
data(), other.data(),
size(), other.size());
175 return memncmp(
data(), other,
size(), std::strlen(other));
191 const auto* ptr =
get();
215 char at(
size_t pos)
const {
219 throw std::out_of_range(
"tvm::String index out of bounds");
228 const char*
data()
const {
return get()->data; }
235 operator std::string()
const {
return std::string{
get()->data,
size()}; }
251 const constexpr uint64_t kMultiplier = 1099511628211ULL;
252 const constexpr uint64_t kMod = 2147483647ULL;
257 static_assert(
sizeof(
Union) ==
sizeof(uint64_t),
"sizeof(Union) != sizeof(uint64_t)");
258 const char* it =
data;
259 const char* end = it +
size;
261 for (; it + 8 <= end; it += 8) {
262 if (DMLC_IO_NO_ENDIAN_SWAP) {
281 result = (result * kMultiplier + u.b) % kMod;
305 if (!DMLC_IO_NO_ENDIAN_SWAP) {
306 std::swap(u.a[0], u.a[7]);
307 std::swap(u.a[1], u.a[6]);
308 std::swap(u.a[2], u.a[5]);
309 std::swap(u.a[3], u.a[4]);
311 result = (result * kMultiplier + u.b) % kMod;
329 static int memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count);
341 static String Concat(
const char* lhs,
size_t lhs_size,
const char* rhs,
size_t rhs_size) {
342 std::string
ret(lhs, lhs_size);
343 ret.append(rhs, rhs_size);
368 explicit FromStd(std::string other) : data_container{other} {}
372 std::string data_container;
378 auto ptr = make_object<StringObj::FromStd>(std::move(other));
379 ptr->size = ptr->data_container.size();
380 ptr->data = ptr->data_container.data();
381 data_ = std::move(ptr);
385 String replace{std::move(other)};
386 data_.swap(replace.data_);
393 size_t lhs_size = lhs.
size();
394 size_t rhs_size = rhs.
size();
399 size_t lhs_size = lhs.
size();
400 size_t rhs_size = rhs.size();
405 size_t lhs_size = lhs.
size();
406 size_t rhs_size = rhs.
size();
411 size_t lhs_size = std::strlen(lhs);
412 size_t rhs_size = rhs.
size();
417 size_t lhs_size = lhs.
size();
418 size_t rhs_size = std::strlen(rhs);
489 out.write(input.
data(), input.
size());
493 inline int String::memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count) {
494 if (lhs == rhs && lhs_count == rhs_count)
return 0;
496 for (
size_t i = 0; i < lhs_count && i < rhs_count; ++i) {
497 if (lhs[i] < rhs[i])
return -1;
498 if (lhs[i] > rhs[i])
return 1;
500 if (lhs_count < rhs_count) {
502 }
else if (lhs_count > rhs_count) {
522 return String::memncmp(str_a->data, str_b->data, str_a->size, str_b->size) == 0;
538 std::size_t operator()(const ::tvm::runtime::String& str)
const {
539 return ::tvm::runtime::String::StableHashBytes(str.data(), str.size());
Base class of all object reference.
Definition: object.h:520
const Object * get() const
Definition: object.h:555
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:606
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:911
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:531
base class of all object containers.
Definition: object.h:172
An object representing string moved from std::string.
Definition: string.h:358
FromStd(std::string other)
Construct a new FromStd object.
Definition: string.h:368
An object representing string. It's POD type.
Definition: string.h:52
const char * data
The pointer to string data.
Definition: string.h:55
static constexpr const char * _type_key
Definition: string.h:61
uint64_t size
The length of the string object.
Definition: string.h:58
static constexpr const uint32_t _type_index
Definition: string.h:60
TVM_DECLARE_FINAL_OBJECT_INFO(StringObj, Object)
Reference to string objects.
Definition: string.h:97
int compare(const char *other) const
Compares this to other.
Definition: string.h:174
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:2675
friend String operator+(const String &lhs, const String &rhs)
Definition: string.h:392
String & operator=(std::string other)
Change the value the reference object points to.
Definition: string.h:384
const char * data() const
Return the data pointer.
Definition: string.h:228
String(const char *other)
Construct a new String object.
Definition: string.h:118
size_t length() const
Return the length of the string.
Definition: string.h:200
static uint64_t StableHashBytes(const char *data, size_t size)
Hash the binary bytes.
Definition: string.h:250
bool empty() const
Retun if the string is empty.
Definition: string.h:207
char at(size_t pos) const
Read an element.
Definition: string.h:215
int compare(const std::string &other) const
Compares this String object to other.
Definition: string.h:162
String()
Construct an empty string.
Definition: string.h:102
const char * c_str() const
Returns a pointer to the char array in the string.
Definition: string.h:183
String(std::nullptr_t)
Construct a new null object.
Definition: string.h:124
int compare(const String &other) const
Compares this String object to other.
Definition: string.h:150
size_t size() const
Return the length of the string.
Definition: string.h:190
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(String, ObjectRef, StringObj)
Runtime memory management.
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:423
String operator+(const String &lhs, const String &rhs)
Definition: string.h:392
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:478
bool operator<=(const String &lhs, const std::string &rhs)
Definition: string.h:445
bool operator>=(const String &lhs, const std::string &rhs)
Definition: string.h:456
bool operator==(const String &lhs, const std::string &rhs)
Definition: string.h:467
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:434
Performance counters for profiling via the PAPI library.
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.
String-aware ObjectRef hash functor.
Definition: base.h:49
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Check if the two ObjectRef are equal.
Definition: string.h:516
size_t operator()(const ObjectRef &a) const
Calculate the hash code of an ObjectRef.
Definition: string.h:509
ObjectRef hash functor.
Definition: object.h:656
@ kRuntimeString
runtime::String.
Definition: object.h:66