microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
base/string_utils.h
77lines · modeblame
c891e5d7Wenbing Li5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
| 3 | #pragma once | |
| 4 | #include <sstream> | |
| 5 | #include <vector> | |
70aa18e1Wenbing Li4 years ago | 6 | #include "ocos.h" |
c891e5d7Wenbing Li5 years ago | 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 << "["; | |
7fc02244Wenbing Li3 years ago | 16 | for (size_t i = 0; i < t.size(); i++) { |
c891e5d7Wenbing Li5 years ago | 17 | if (i != 0) { |
| 18 | ss << ", "; | |
| 19 | } | |
| 20 | ss << t[i]; | |
| 21 | } | |
| 22 | ss << "]"; | |
| 23 | } | |
| 24 | | |
70aa18e1Wenbing Li4 years ago | 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 | | |
c891e5d7Wenbing Li5 years ago | 30 | template <> |
| 31 | inline void MakeStringInternal(std::ostringstream& ss, const std::vector<std::string>& t) noexcept { | |
| 32 | ss << "["; | |
7fc02244Wenbing Li3 years ago | 33 | for (size_t i = 0; i < t.size(); i++) { |
c891e5d7Wenbing Li5 years ago | 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 | | |
aef5ef1eMojimi4 years ago | 57 | bool IsCJK(char32_t c); |
| 58 | | |
| 59 | bool IsAccent(char32_t c); | |
| 60 | | |
44851853Mojimi4 years ago | 61 | bool IsSpace(char32_t c); |
| 62 | | |
| 63 | bool IsPunct(char32_t c); | |
| 64 | | |
| 65 | bool IsControl(char32_t c); | |
| 66 | | |
46d096f1Mojimi4 years ago | 67 | char32_t ToLower(char32_t c); |
| 68 | | |
aef5ef1eMojimi4 years ago | 69 | char32_t StripAccent(char32_t c); |
c891e5d7Wenbing Li5 years ago | 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); |