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 <string_view> 40 #include <type_traits> 41 #include <unordered_map> 61 static constexpr
const char*
_type_key =
"runtime.String";
111 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");
235 operator std::string()
const {
return std::string{
get()->
data,
size()}; }
242 inline static bool CanConvertFrom(
const TVMArgValue& val);
253 return std::hash<std::string_view>()(std::string_view(data, size));
269 static int memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count);
281 static String Concat(
const char* lhs,
size_t lhs_size,
const char* rhs,
size_t rhs_size) {
282 std::string
ret(lhs, lhs_size);
283 ret.append(rhs, rhs_size);
308 explicit FromStd(std::string other) : data_container{other} {}
312 std::string data_container;
318 auto ptr = make_object<StringObj::FromStd>(std::move(other));
319 ptr->size = ptr->data_container.size();
320 ptr->data = ptr->data_container.data();
321 data_ = std::move(ptr);
325 String replace{std::move(other)};
326 data_.swap(replace.data_);
333 size_t lhs_size = lhs.
size();
334 size_t rhs_size = rhs.
size();
339 size_t lhs_size = lhs.
size();
340 size_t rhs_size = rhs.size();
345 size_t lhs_size = lhs.
size();
346 size_t rhs_size = rhs.
size();
351 size_t lhs_size = std::strlen(lhs);
352 size_t rhs_size = rhs.
size();
357 size_t lhs_size = lhs.
size();
358 size_t rhs_size = std::strlen(rhs);
429 out.write(input.
data(), input.
size());
433 inline int String::memncmp(
const char* lhs,
const char* rhs,
size_t lhs_count,
size_t rhs_count) {
434 if (lhs == rhs && lhs_count == rhs_count)
return 0;
436 for (
size_t i = 0; i < lhs_count && i < rhs_count; ++i) {
437 if (lhs[i] < rhs[i])
return -1;
438 if (lhs[i] > rhs[i])
return 1;
440 if (lhs_count < rhs_count) {
442 }
else if (lhs_count > rhs_count) {
462 return String::memncmp(str_a->data, str_b->data, str_a->size, str_b->size) == 0;
478 std::size_t operator()(const ::tvm::runtime::String& str)
const {
479 return ::tvm::runtime::String::HashBytes(str.data(), str.size());
484 #endif // TVM_RUNTIME_CONTAINER_STRING_H_ std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:69
uint64_t size
The length of the string object.
Definition: string.h:58
String-aware ObjectRef hash functor.
Definition: base.h:50
int compare(const char *other) const
Compares this to other.
Definition: string.h:174
String(std::nullptr_t)
Construct a new null object.
Definition: string.h:124
char at(size_t pos) const
Read an element.
Definition: string.h:215
const char * data() const
Return the data pointer.
Definition: string.h:228
An object representing string moved from std::string.
Definition: string.h:298
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
TVM_DECLARE_FINAL_OBJECT_INFO(StringObj, Object)
runtime::String.
Definition: object.h:66
String()
Construct an empty string.
Definition: string.h:102
bool operator>(const String &lhs, const std::string &rhs)
Definition: string.h:374
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:250
Base utilities for common POD(plain old data) container types.
size_t operator()(const ObjectRef &a) const
Calculate the hash code of an ObjectRef.
Definition: string.h:449
static constexpr const uint32_t _type_index
Definition: string.h:60
base class of all object containers.
Definition: object.h:167
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Check if the two ObjectRef are equal.
Definition: string.h:456
size_t length() const
Return the length of the string.
Definition: string.h:200
static constexpr const char * _type_key
Definition: string.h:61
bool operator>=(const String &lhs, const std::string &rhs)
Definition: string.h:396
const char * c_str() const
Returns a pointer to the char array in the string.
Definition: string.h:183
const char * data
The pointer to string data.
Definition: string.h:55
bool operator!=(const String &lhs, const std::string &rhs)
Definition: string.h:418
FromStd(std::string other)
Construct a new FromStd object.
Definition: string.h:308
friend class String
Definition: string.h:66
ObjectRef hash functor.
Definition: object.h:624
String operator+(const String &lhs, const String &rhs)
Definition: string.h:332
size_t size() const
Return the length of the string.
Definition: string.h:190
Reference to string objects.
Definition: string.h:97
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:522
bool operator<=(const String &lhs, const std::string &rhs)
Definition: string.h:385
String & operator=(std::string other)
Change the value the reference object points to.
Definition: string.h:324
Object & operator=(const Object &other)
Definition: object.h:251
bool operator<(const String &lhs, const std::string &rhs)
Definition: string.h:363
Base class of all object reference.
Definition: object.h:511
A managed object in the TVM runtime.
An object representing string. It's POD type.
Definition: string.h:52
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
String(const char *other)
Construct a new String object.
Definition: string.h:118
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:646
bool empty() const
Retun if the string is empty.
Definition: string.h:207
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:865
int compare(const String &other) const
Compares this String object to other.
Definition: string.h:150
bool operator==(const String &lhs, const std::string &rhs)
Definition: string.h:407
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
Array< T > Concat(Array< T > lhs, const Array< T > &rhs)
Concat two Arrays.
Definition: array.h:840
int compare(const std::string &other) const
Compares this String object to other.
Definition: string.h:162