Specifiy that this supports M47
[platform/framework/web/crosswalk-tizen.git] / common / string_utils.cc
1 /*
2  * Copyright (c) 2015 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 #include "common/string_utils.h"
18
19 #include <uuid/uuid.h>
20 #include <glib.h>
21 #include <math.h>
22 #include <time.h>
23
24 #include <algorithm>
25 #include <iomanip>
26 #include <memory>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30
31 namespace common {
32 namespace utils {
33
34 std::string GenerateUUID() {
35   char tmp[37];
36   uuid_t uuid;
37   uuid_generate(uuid);
38   uuid_unparse(uuid, tmp);
39   return std::string(tmp);
40 }
41
42 bool StartsWith(const std::string& str, const std::string& sub) {
43   if (sub.size() > str.size()) return false;
44   return std::equal(sub.begin(), sub.end(), str.begin());
45 }
46
47 bool EndsWith(const std::string& str, const std::string& sub) {
48   if (sub.size() > str.size()) return false;
49   return std::equal(sub.rbegin(), sub.rend(), str.rbegin());
50 }
51
52 std::string ReplaceAll(const std::string& replace,
53                        const std::string& from, const std::string& to) {
54   std::string str = replace;
55   size_t pos = str.find(from);
56   while (pos != std::string::npos) {
57     str.replace(pos, from.length(), to);
58     pos = str.find(from, pos+to.length());
59   }
60   return str;
61 }
62
63 std::string GetCurrentMilliSeconds() {
64   std::ostringstream ss;
65   struct timespec spec;
66   clock_gettime(CLOCK_REALTIME, &spec);
67   ss << (spec.tv_sec%10000) << "." <<
68      std::setw(3) << std::setfill('0') << (round(spec.tv_nsec / 1.0e6));
69   return ss.str();
70 }
71
72 bool SplitString(const std::string &str,
73                  std::string *part_1, std::string *part_2, const char delim) {
74   if (part_1 == nullptr || part_2 == nullptr)
75     return false;
76
77   size_t pos = str.find(delim);
78   if (pos == std::string::npos)
79     return false;
80
81   *part_1 = str.substr(0, pos);
82   *part_2 = str.substr(pos+1);
83   return true;
84 }
85
86 std::string UrlDecode(const std::string& url) {
87   std::unique_ptr<char, decltype(std::free)*> decoded_str {
88       g_uri_unescape_string(url.c_str(), NULL), std::free };
89   return decoded_str.get() != nullptr ? std::string(decoded_str.get()) : url;
90 }
91
92 std::string UrlEncode(const std::string& url) {
93   std::unique_ptr<char, decltype(std::free)*> encoded_str {
94       g_uri_escape_string(url.c_str(), NULL, TRUE), std::free };
95   return encoded_str.get() != nullptr ? std::string(encoded_str.get()) : url;
96 }
97
98 std::string Base64Encode(const unsigned char* data, size_t len) {
99   gchar* encoded = g_base64_encode(data, len);
100   std::unique_ptr<gchar, decltype(g_free)*> encoded_ptr {encoded, g_free};
101   return std::string(encoded);
102 }
103
104 }  // namespace utils
105 }  // namespace common