You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(string): add String::Split, EscapedStringPy, and rename EscapeString (#550)
## Summary
- Rename `EscapeString` to `EscapeStringJSON` to clarify its
JSON-specific escaping semantics (RFC 8259). A deprecated `EscapeString`
alias is retained for backward compatibility.
- Add `EscapedStringPy` for Python-style string escaping that handles
ANSI escape sequences, UTF-8 multibyte characters, and standard C escape
sequences (`\n`, `\t`, `\r`, `\\`, `\"`).
- Add `String::Split(char delim)` utility method that returns
`std::vector<std::string_view>` segments.
- Update all internal call sites (`function.h`, `registry.h`,
`dataclass.cc`, `json_writer.cc`) to use the new `EscapeStringJSON`
name.
- `ReprPrinter` now uses `EscapedStringPy` instead of `EscapeStringJSON`
for proper Python-style `__repr__` output.
## Motivation
The existing `EscapeString` function was JSON-specific but its name did
not convey this. This rename makes intent explicit. The new
`EscapedStringPy` function supports Python-style repr output needed for
error messages and debugging. `String::Split` is a common utility needed
across the codebase.
## Changes
| File | Change |
|------|--------|
| `include/tvm/ffi/string.h` | Rename `EscapeString` ->
`EscapeStringJSON`, add deprecated alias, add `EscapedStringPy`, add
`String::Split` |
| `include/tvm/ffi/string.h` | Cast to `unsigned char` before
`std::isdigit` to avoid UB; use `\x1b` for ANSI escapes; validate UTF-8
continuation bytes |
| `include/tvm/ffi/function.h` | Update call site to `EscapeStringJSON`
|
| `include/tvm/ffi/reflection/registry.h` | Update call site to
`EscapeStringJSON` |
| `src/ffi/extra/dataclass.cc` | `ReprPrinter` uses `EscapedStringPy`
for Python-style repr output |
| `src/ffi/extra/json_writer.cc` | Update call site to
`EscapeStringJSON` |
| `tests/cpp/test_string.cc` | Add 7 test cases for `Split`,
`EscapeStringJSON`, `EscapedStringPy` (basic, control chars, ANSI,
UTF-8, malformed UTF-8) |
## Test plan
- [x] All 47 C++ string tests pass
- [x] `String::Split` tested with edge cases (empty, boundaries,
consecutive delimiters)
- [x] `EscapeStringJSON` tested with special chars, backslash, quotes,
control chars
- [x] `EscapedStringPy` tested: basic ASCII, control chars, ANSI
sequences, valid UTF-8 (2/3/4-byte), malformed UTF-8
- [x] Existing Python tests pass (deprecated alias preserves
compatibility)
0 commit comments