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