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
19 new_cstr_char_array (const std::string
&s
)
21 size_t len
= s
.size();
22 char *buf
= new char[len
+ 1];
24 memcpy(buf
, s
.c_str(), len
);
30 format_1000s (int num
, char sep
)
33 while (mult
* 1000 < num
)
35 std::ostringstream os
;
36 os
<< ((num
/ mult
) % 1000);
37 for (mult
/= 1000; mult
> 0; mult
/= 1000)
39 int triplet
= (num
/ mult
) % 1000;
41 if (triplet
< 100) os
<< '0';
42 if (triplet
< 10) os
<< '0';
51 std::ostringstream os
;
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
;
64 for (i
= 0; i
< length_to_check
; ++i
)
65 if (toupper(a
[i
]) < toupper(b
[i
]))
67 else if (toupper(a
[i
]) > toupper(b
[i
]))
70 // Hit the comparison limit without finding a difference
71 if (limit
&& i
== limit
)
74 if (a
.length() < b
.length())
76 else if (a
.length() > b
.length())
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(),
92 size_t pos
= rv
.find(needle
, search_start
);
93 if (pos
== std::string::npos
)
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);
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
);
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
);
123 return "conversion failed";
125 char *buf
= new char[n
+1];
126 WideCharToMultiByte(encoding
, 0, w
.c_str(), -1, buf
, n
, NULL
, NULL
);
135 vformat(const std::wstring
&fmt
, va_list ap
)
140 int n
= vsnwprintf(NULL
, 0, fmt
.c_str(), ap
);
144 vsnwprintf(&str
[0], n
+1, fmt
.c_str(), apc
);
148 // discard terminating null written by vsnwprintf from std::string
156 format(const std::wstring
&fmt
, ...)
160 std::wstring res
= vformat(fmt
, ap
);