Add new option --no-write-registry
[cygwin-setup.git] / String++.cc
blob9506b9e3d231fbaf5b682e7d1aa62232bdd90cfa
1 /*
2 * Copyright 2005-2006, Various Contributors.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
13 #include "String++.h"
14 #include <string.h>
15 #include <sstream>
16 #include <algorithm>
18 char *
19 new_cstr_char_array (const std::string &s)
21 size_t len = s.size();
22 char *buf = new char[len + 1];
23 if (len)
24 memcpy(buf, s.c_str(), len);
25 buf[len] = 0;
26 return buf;
29 std::string
30 format_1000s (int num, char sep)
32 int mult = 1;
33 while (mult * 1000 < num)
34 mult *= 1000;
35 std::ostringstream os;
36 os << ((num / mult) % 1000);
37 for (mult /= 1000; mult > 0; mult /= 1000)
39 int triplet = (num / mult) % 1000;
40 os << sep;
41 if (triplet < 100) os << '0';
42 if (triplet < 10) os << '0';
43 os << triplet;
45 return os.str();
48 std::string
49 stringify (int num)
51 std::ostringstream os;
52 os << num;
53 return os.str();
56 int
57 casecompare (const std::string& a, const std::string& b, size_t limit)
59 size_t length_to_check = std::min(a.length(), b.length());
60 if (limit && length_to_check > limit)
61 length_to_check = limit;
63 size_t i;
64 for (i = 0; i < length_to_check; ++i)
65 if (toupper(a[i]) < toupper(b[i]))
66 return -1;
67 else if (toupper(a[i]) > toupper(b[i]))
68 return 1;
70 // Hit the comparison limit without finding a difference
71 if (limit && i == limit)
72 return 0;
74 if (a.length() < b.length())
75 return -1;
76 else if (a.length() > b.length())
77 return 1;
79 return 0;
82 std::string
83 replace(const std::string& haystack, const std::string& needle,
84 const std::string& replacement)
86 std::string rv(haystack);
87 size_t n_len = needle.length(), r_len = replacement.length(),
88 search_start = 0;
90 while (true)
92 size_t pos = rv.find(needle, search_start);
93 if (pos == std::string::npos)
94 return rv;
95 rv.replace(pos, n_len, replacement);
96 search_start = pos + r_len;
100 // convert a UTF-8 string to a UTF-16 wstring
101 std::wstring string_to_wstring(const std::string &s)
103 int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
105 if (n <= 0)
106 return L"conversion failed";
108 wchar_t *buf = new wchar_t[n+1];
109 MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, n);
111 std::wstring w(buf);
112 delete[] buf;
114 return w;
117 // convert a UTF-16 wstring to a UTF-8 string
118 std::string wstring_to_string(const std::wstring &w, unsigned int encoding)
120 int n = WideCharToMultiByte(encoding, 0, w.c_str(), -1, NULL, 0, NULL, NULL);
122 if (n <= 0)
123 return "conversion failed";
125 char *buf = new char[n+1];
126 WideCharToMultiByte(encoding, 0, w.c_str(), -1, buf, n, NULL, NULL);
128 std::string s(buf);
129 delete[] buf;
131 return s;
134 std::wstring
135 vformat(const std::wstring &fmt, va_list ap)
137 va_list apc;
138 va_copy(apc, ap);
140 int n = vsnwprintf(NULL, 0, fmt.c_str(), ap);
142 std::wstring str;
143 str.resize(n+1);
144 vsnwprintf(&str[0], n+1, fmt.c_str(), apc);
146 va_end(apc);
148 // discard terminating null written by vsnwprintf from std::string
149 if (str[n] == 0)
150 str.resize(n);
152 return str;
155 std::wstring
156 format(const std::wstring &fmt, ...)
158 va_list ap;
159 va_start(ap, fmt);
160 std::wstring res = vformat(fmt, ap);
161 va_end(ap);
162 return res;