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
20 new_cstr_char_array (const std::string
&s
)
22 size_t len
= s
.size();
23 char *buf
= new char[len
+ 1];
25 memcpy(buf
, s
.c_str(), len
);
31 format_1000s (int num
, char sep
)
34 while (mult
* 1000 < num
)
36 std::ostringstream os
;
37 os
<< ((num
/ mult
) % 1000);
38 for (mult
/= 1000; mult
> 0; mult
/= 1000)
40 int triplet
= (num
/ mult
) % 1000;
42 if (triplet
< 100) os
<< '0';
43 if (triplet
< 10) os
<< '0';
52 std::ostringstream os
;
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
;
65 for (i
= 0; i
< length_to_check
; ++i
)
66 if (toupper(a
[i
]) < toupper(b
[i
]))
68 else if (toupper(a
[i
]) > toupper(b
[i
]))
71 // Hit the comparison limit without finding a difference
72 if (limit
&& i
== limit
)
75 if (a
.length() < b
.length())
77 else if (a
.length() > b
.length())
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(),
93 size_t pos
= rv
.find(needle
, search_start
);
94 if (pos
== std::string::npos
)
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);
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
);
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
);
124 return "conversion failed";
126 char *buf
= new char[n
+1];
127 WideCharToMultiByte(CP_UTF8
, 0, w
.c_str(), -1, buf
, n
, NULL
, NULL
);
136 vformat(const std::wstring
&fmt
, va_list ap
)
141 int n
= vsnwprintf(NULL
, 0, fmt
.c_str(), ap
);
145 vsnwprintf(&str
[0], n
+1, fmt
.c_str(), apc
);