Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / libs / misc / include / misc / string_helpers.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file string_helpers.h
19  * @ingroup COM_AI_RUNTIME
20  * @brief This file contains helper functions for std::string
21  */
22
23 #include <ostream>
24 #include <string>
25 #include <sstream>
26 #include <vector>
27
28 namespace
29 {
30
31 template <typename Arg> void _str(std::ostream &os, Arg &&arg) { os << std::forward<Arg>(arg); }
32
33 template <typename Arg, typename... Args> void _str(std::ostream &os, Arg &&arg, Args &&... args)
34 {
35   _str(os, std::forward<Arg>(arg));
36   _str(os, std::forward<Args>(args)...);
37 }
38
39 } // namespace
40
41 namespace nnfw
42 {
43 namespace misc
44 {
45
46 inline std::vector<std::string> split(const std::string &s, char delim)
47 {
48   std::stringstream ss(s);
49   std::string item;
50   std::vector<std::string> elems;
51   while (std::getline(ss, item, delim))
52   {
53     elems.push_back(std::move(item));
54   }
55   return elems;
56 }
57
58 template <typename... Args> std::string str(Args &&... args)
59 {
60   std::stringstream ss;
61   _str(ss, std::forward<Args>(args)...);
62   return ss.str();
63 }
64
65 template <typename InputIt> std::string join(InputIt first, InputIt last, const std::string &concat)
66 {
67   std::string ret;
68   if (first == last)
69     return ret;
70
71   ret += *first;
72   for (++first; first != last; ++first)
73   {
74     ret += concat;
75     ret += *first;
76   }
77   return ret;
78 }
79
80 } // namespace misc
81 } // namespace nnfw