Adjustment to dialog layout for translation
[cygwin-setup.git] / String++.cc
bloba5649759c4d12a5c201f674f0f871e337efc5310
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 "win32.h"
15 #include <string.h>
16 #include <sstream>
17 #include <algorithm>
19 char *
20 new_cstr_char_array (const std::string &s)
22 size_t len = s.size();
23 char *buf = new char[len + 1];
24 if (len)
25 memcpy(buf, s.c_str(), len);
26 buf[len] = 0;
27 return buf;
30 std::string
31 format_1000s (int num, char sep)
33 int mult = 1;
34 while (mult * 1000 < num)
35 mult *= 1000;
36 std::ostringstream os;
37 os << ((num / mult) % 1000);
38 for (mult /= 1000; mult > 0; mult /= 1000)
40 int triplet = (num / mult) % 1000;
41 os << sep;
42 if (triplet < 100) os << '0';
43 if (triplet < 10) os << '0';
44 os << triplet;
46 return os.str();
49 std::string
50 stringify (int num)
52 std::ostringstream os;
53 os << num;
54 return os.str();
57 int
58 casecompare (const std::string& a, const std::string& b, size_t limit)
60 size_t length_to_check = std::min(a.length(), b.length());
61 if (limit && length_to_check > limit)
62 length_to_check = limit;
64 size_t i;
65 for (i = 0; i < length_to_check; ++i)
66 if (toupper(a[i]) < toupper(b[i]))
67 return -1;
68 else if (toupper(a[i]) > toupper(b[i]))
69 return 1;
71 // Hit the comparison limit without finding a difference
72 if (limit && i == limit)
73 return 0;
75 if (a.length() < b.length())
76 return -1;
77 else if (a.length() > b.length())
78 return 1;
80 return 0;
83 std::string
84 replace(const std::string& haystack, const std::string& needle,
85 const std::string& replacement)
87 std::string rv(haystack);
88 size_t n_len = needle.length(), r_len = replacement.length(),
89 search_start = 0;
91 while (true)
93 size_t pos = rv.find(needle, search_start);
94 if (pos == std::string::npos)
95 return rv;
96 rv.replace(pos, n_len, replacement);
97 search_start = pos + r_len;
101 // convert a UTF-8 string to a UTF-16 wstring
102 std::wstring string_to_wstring(const std::string &s)
104 int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
106 if (n <= 0)
107 return L"conversion failed";
109 wchar_t *buf = new wchar_t[n+1];
110 MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, n);
112 std::wstring w(buf);
113 delete[] buf;
115 return w;
118 // convert a UTF-16 wstring to a UTF-8 string
119 std::string wstring_to_string(const std::wstring &w)
121 int n = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, NULL, 0, NULL, NULL);
123 if (n <= 0)
124 return "conversion failed";
126 char *buf = new char[n+1];
127 WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, buf, n, NULL, NULL);
129 std::string s(buf);
130 delete[] buf;
132 return s;
135 std::wstring
136 vformat(const std::wstring &fmt, va_list ap)
138 va_list apc;
139 va_copy(apc, ap);
141 int n = vsnwprintf(NULL, 0, fmt.c_str(), ap);
143 std::wstring str;
144 str.resize(n+1);
145 vsnwprintf(&str[0], n+1, fmt.c_str(), apc);
147 va_end(apc);
149 // discard terminating null written by vsnwprintf from std::string
150 if (str[n] == 0)
151 str.resize(n);
153 return str;
156 std::wstring
157 format(const std::wstring &fmt, ...)
159 va_list ap;
160 va_start(ap, fmt);
161 std::wstring res = vformat(fmt, ap);
162 va_end(ap);
163 return res;