microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
base/string_utils.h
77lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | #pragma once |
| 4 | #include <sstream> |
| 5 | #include <vector> |
| 6 | #include "ort_c_to_cpp.h" |
| 7 | |
| 8 | template <typename T> |
| 9 | inline void MakeStringInternal(std::ostringstream& ss, const T& t) noexcept { |
| 10 | ss << t; |
| 11 | } |
| 12 | |
| 13 | template <> |
| 14 | inline void MakeStringInternal(std::ostringstream& ss, const std::vector<int64_t>& t) noexcept { |
| 15 | ss << "["; |
| 16 | for (size_t i = 0; i < t.size(); i++) { |
| 17 | if (i != 0) { |
| 18 | ss << ", "; |
| 19 | } |
| 20 | ss << t[i]; |
| 21 | } |
| 22 | ss << "]"; |
| 23 | } |
| 24 | |
| 25 | template <> |
| 26 | inline void MakeStringInternal(std::ostringstream& ss, const OrtTensorDimensions& t) noexcept { |
| 27 | MakeStringInternal(ss, static_cast<const std::vector<int64_t>&>(t)); |
| 28 | } |
| 29 | |
| 30 | template <> |
| 31 | inline void MakeStringInternal(std::ostringstream& ss, const std::vector<std::string>& t) noexcept { |
| 32 | ss << "["; |
| 33 | for (size_t i = 0; i < t.size(); i++) { |
| 34 | if (i != 0) { |
| 35 | ss << ", "; |
| 36 | } |
| 37 | ss << t[i]; |
| 38 | } |
| 39 | ss << "]"; |
| 40 | } |
| 41 | |
| 42 | template <typename T, typename... Args> |
| 43 | void MakeStringInternal(std::ostringstream& ss, const T& t, const Args&... args) noexcept { |
| 44 | MakeStringInternal(ss, t); |
| 45 | MakeStringInternal(ss, args...); |
| 46 | } |
| 47 | |
| 48 | template <typename... Args> |
| 49 | std::string MakeString(const Args&... args) { |
| 50 | std::ostringstream ss; |
| 51 | MakeStringInternal(ss, args...); |
| 52 | return std::string(ss.str()); |
| 53 | } |
| 54 | |
| 55 | std::vector<std::string_view> SplitString(const std::string_view& str, const std::string_view& seps, bool remove_empty_entries = false); |
| 56 | |
| 57 | bool IsCJK(char32_t c); |
| 58 | |
| 59 | bool IsAccent(char32_t c); |
| 60 | |
| 61 | bool IsSpace(char32_t c); |
| 62 | |
| 63 | bool IsPunct(char32_t c); |
| 64 | |
| 65 | bool IsControl(char32_t c); |
| 66 | |
| 67 | char32_t ToLower(char32_t c); |
| 68 | |
| 69 | char32_t StripAccent(char32_t c); |
| 70 | |
| 71 | uint64_t Hash64(const char* data, size_t n, uint64_t seed); |
| 72 | |
| 73 | inline uint64_t Hash64(const char* data, size_t n) { |
| 74 | return Hash64(data, n, 0xDECAFCAFFE); |
| 75 | } |
| 76 | |
| 77 | uint64_t Hash64Fast(const char* data, size_t n); |