1 /*
2  * Copyright (c) 2021 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 
16 #include "string_utils.h"
17 
18 namespace OHOS {
19 namespace NativeRdb {
SurroundWithQuote(const std::string & value,const std::string & quote)20 std::string StringUtils::SurroundWithQuote(const std::string &value, const std::string &quote)
21 {
22     if (value.empty()) {
23         return value;
24     }
25     return quote + value + quote;
26 }
27 
28 // Join array members as parameters of a function call.
SurroundWithFunction(const std::string & function,const std::string & separator,const std::vector<std::string> & array)29 std::string StringUtils::SurroundWithFunction(const std::string &function, const std::string &separator,
30     const std::vector<std::string> &array)
31 {
32     std::string builder(function);
33     builder += "(";
34     bool isFirst = true;
35     for (const auto &text : array) {
36         if (!isFirst) {
37             builder = builder + " " + separator + " ";
38         } else {
39             isFirst = false;
40         }
41         builder += text;
42     }
43     builder += ")";
44     return builder;
45 }
46 
Split(const std::string & str,const std::string & delim)47 std::vector<std::string> StringUtils::Split(const std::string &str, const std::string &delim)
48 {
49     std::vector<std::string> res;
50     size_t pos = 0;
51     while (pos < str.size()) {
52         size_t found = str.find(delim, pos);
53         if (found == std::string::npos) {
54             res.push_back(str.substr(pos));
55             break;
56         }
57         res.push_back(str.substr(pos, found - pos));
58         pos = found + delim.size();
59     }
60     return res;
61 }
62 
ExtractFilePath(const std::string & fileFullName)63 std::string StringUtils::ExtractFilePath(const std::string& fileFullName)
64 {
65     return std::string(fileFullName).substr(0, fileFullName.rfind("/") + 1);
66 }
67 
ExtractFileName(const std::string & fileFullName)68 std::string StringUtils::ExtractFileName(const std::string& fileFullName)
69 {
70     return std::string(fileFullName).substr(fileFullName.rfind("/") + 1, fileFullName.size());
71 }
72 
StringUtils()73 StringUtils::StringUtils() {}
~StringUtils()74 StringUtils::~StringUtils() {}
75 } // namespace NativeRdb
76 } // namespace OHOS