• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "taihe/string.hpp"
16 
17 #include "stdexcept"
18 #include "string_op.PlayString.proj.2.hpp"
19 #include "string_op.StringPair.proj.1.hpp"
20 #include "string_op.impl.hpp"
21 #include "taihe/array.hpp"
22 // Please delete <stdexcept> include when you implement
23 using namespace taihe;
24 
25 namespace {
26 class PlayString {
27     string name_ {"PlayString"};
28 
29 public:
pickString(array_view<string> nums,int32_t n1,int32_t n2)30     string pickString(array_view<string> nums, int32_t n1, int32_t n2)
31     {
32         int32_t size = static_cast<int32_t>(nums.size());
33         if (n1 > n2 || n1 < 0 || n2 >= size) {
34             throw std::runtime_error("index error");
35         }
36         string res {};
37         for (int32_t i = n1; i <= n2; i++) {
38             res = res + nums[i];
39         }
40         return res;
41     }
42 
getName()43     string getName()
44     {
45         return name_;
46     }
47 
setName(string_view name)48     void setName(string_view name)
49     {
50         name_ = name;
51     }
52 };
53 
concatString(string_view a,string_view b)54 string concatString(string_view a, string_view b)
55 {
56     return a + b;
57 }
58 
makeString(string_view a,int32_t b)59 string makeString(string_view a, int32_t b)
60 {
61     string result = "";
62     while (b-- > 0) {
63         result = result + a;
64     }
65     return result;
66 }
67 
split(string_view a,int32_t n)68 ::string_op::StringPair split(string_view a, int32_t n)
69 {
70     int32_t l = a.size();
71     if (n > l) {
72         n = l;
73     } else if (n + l < 0) {
74         n = 0;
75     } else if (n < 0) {
76         n = n + l;
77     }
78     return {
79         a.substr(0, n),
80         a.substr(n, l - n),
81     };
82 }
83 
split2(string_view a,int32_t n)84 array<string> split2(string_view a, int32_t n)
85 {
86     int32_t l = a.size();
87     if (n > l) {
88         n = l;
89     } else if (n + l < 0) {
90         n = 0;
91     } else if (n < 0) {
92         n = n + l;
93     }
94     return {a.substr(0, n), a.substr(n, l - n)};
95 }
96 
to_i32(string_view a)97 int32_t to_i32(string_view a)
98 {
99     return std::atoi(a.c_str());
100 }
101 
from_i32(int32_t a)102 string from_i32(int32_t a)
103 {
104     return to_string(a);
105 }
106 
makePlayStringIface()107 ::string_op::PlayString makePlayStringIface()
108 {
109     return make_holder<PlayString, ::string_op::PlayString>();
110 }
111 
to_f32(string_view a)112 float to_f32(string_view a)
113 {
114     return std::atof(a.c_str());
115 }
116 
from_f32(float a)117 string from_f32(float a)
118 {
119     return to_string(a);
120 }
121 
concatString2(string_view s,int32_t n,array_view<string> sArr,bool b,array_view<uint8_t> buffer)122 string concatString2(string_view s, int32_t n, array_view<string> sArr, bool b, array_view<uint8_t> buffer)
123 {
124     string result = "";
125     for (auto i = 0; i < n; i++) {
126         result = result + s;
127     }
128     if (b) {
129         for (auto c : sArr) {
130             result = result + c;
131         }
132         for (auto j : buffer) {
133             result = result + to_string(j);
134         }
135     }
136     return result;
137 }
138 }  // namespace
139 
140 // because these macros are auto-generate, lint will cause false positive.
141 // NOLINTBEGIN
142 TH_EXPORT_CPP_API_concatString(concatString);
143 TH_EXPORT_CPP_API_makeString(makeString);
144 TH_EXPORT_CPP_API_split(split);
145 TH_EXPORT_CPP_API_split2(split2);
146 TH_EXPORT_CPP_API_to_i32(to_i32);
147 TH_EXPORT_CPP_API_from_i32(from_i32);
148 TH_EXPORT_CPP_API_makePlayStringIface(makePlayStringIface);
149 TH_EXPORT_CPP_API_to_f32(to_f32);
150 TH_EXPORT_CPP_API_from_f32(from_f32);
151 TH_EXPORT_CPP_API_concatString2(concatString2);
152 // NOLINTEND