620f66380a0e95afcf263303b834453221c964db
[platform/framework/web/crosswalk-tizen.git] / src / common / string_utils.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "common/string_utils.h"
6
7 #include <time.h>
8 #include <math.h>
9 #include <uuid/uuid.h>
10 #include <string>
11 #include <sstream>
12 #include <iomanip>
13 #include <algorithm>
14
15 namespace wrt {
16 namespace utils {
17
18 std::string GenerateUUID() {
19   char tmp[37];
20   uuid_t uuid;
21   uuid_generate(uuid);
22   uuid_unparse(uuid, tmp);
23   return std::string(tmp);
24 }
25
26 bool StartsWith(const std::string& str, const std::string& sub) {
27   if (sub.size() > str.size()) return false;
28   return std::equal(sub.begin(), sub.end(), str.begin());
29 }
30
31 bool EndsWith(const std::string& str, const std::string& sub) {
32   if (sub.size() > str.size()) return false;
33   return std::equal(sub.rbegin(), sub.rend(), str.rbegin());
34 }
35
36 std::string ReplaceAll(const std::string& replace,
37                        const std::string& from, const std::string& to) {
38   std::string str = replace;
39   size_t pos = str.find(from);
40   while (pos != std::string::npos) {
41     str.replace(pos, from.length(), to);
42     pos = str.find(from, pos+to.length());
43   }
44   return str;
45 }
46
47 std::string GetCurrentMilliSeconds() {
48   std::ostringstream ss;
49   struct timespec spec;
50   clock_gettime(CLOCK_REALTIME, &spec);
51   ss << spec.tv_sec << "." <<
52      std::setw(3) << std::setfill('0') << (round(spec.tv_nsec / 1.0e6));
53   return ss.str();
54 }
55
56 }  // namespace utils
57 }  // namespace wrt