OSDN Git Service

Fix osdn.net #42179: Thicken the caret in overwrite mode
[winmerge-jp/winmerge-jp.git] / ShellExtension / UnicodeString.h
1 /////////////////////////////////////////////////////////////////////////////
2 //    License (GPLv2+):
3 //    This program is free software; you can redistribute it and/or modify
4 //    it under the terms of the GNU General Public License as published by
5 //    the Free Software Foundation; either version 2 of the License, or
6 //    (at your option) any later version.
7 //
8 //    This program is distributed in the hope that it will be useful, but
9 //    WITHOUT ANY WARRANTY; without even the implied warranty of
10 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //    General Public License for more details.
12 //
13 //    You should have received a copy of the GNU General Public License
14 //    along with this program; if not, write to the Free Software
15 //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 /////////////////////////////////////////////////////////////////////////////
17
18 /** 
19  * @file UnicodeString.h
20  *
21  * @brief Unicode string based on std::wstring.
22  *
23  */
24 #pragma once
25
26 #include <string>
27 #include <cstdarg>
28 #include <cstdint>
29
30 #ifdef _WIN32
31 #  include <tchar.h>
32 #else
33 #  ifndef _T
34 #    ifdef _UNICODE
35 #      define _T(x) L ## x
36 #    else
37 #      define _T(x) x
38 #    endif
39 #  endif
40 #  ifndef _TCHAR_DEFINED
41 #    ifdef _UNICODE
42 typedef wchar_t TCHAR;
43 #    else
44 typedef char    TCHAR;
45 #    endif
46 #  endif
47 #  define _TCHAR_DEFINED
48 #endif
49
50 #ifdef _UNICODE
51 #define std_tchar(type) std::w##type
52 #else
53 #define std_tchar(type) std::type
54 #endif // _UNICODE
55
56 typedef std_tchar(string) String;
57
58 namespace strutils
59 {
60
61 String makelower(const String &str);
62 String makeupper(const String &str);
63
64 void replace(String &target, const String &find, const String &replace);
65
66 // Comparing
67 int compare_nocase(const String &str1, const String &str2);
68
69 // Trimming
70 String trim_ws(const String & str);
71 String trim_ws_begin(const String & str);
72 String trim_ws_end(const String & str);
73
74 // Formatting
75 String format_arg_list(const TCHAR *fmt, va_list args);
76 String format(const TCHAR *fmt, ...);
77 String format_strings(const String& fmt, const String *args[], size_t nargs);
78 String format_string1(const String& fmt, const String& arg1);
79 String format_string2(const String& fmt, const String& arg1, const String& arg2);
80
81 template <class InputIterator>
82 String join(const InputIterator& begin, const InputIterator& end, const String& delim)
83 {
84         size_t sum = 0, delim_len = delim.length();
85         for (InputIterator it = begin; it != end; ++it)
86         {
87                 if (sum != 0) sum += delim_len;
88                 sum += (*it).length();
89         }
90         String result;
91         result.reserve(sum);
92         for (InputIterator it = begin; it != end; ++it)
93         {
94                 if (!result.empty()) result.append(delim);
95                 result += *it;
96         }
97         return result;
98 }
99
100 template <class Formatter, class InputIterator>
101 String join(const InputIterator& begin, const InputIterator& end, const String& delim, Formatter func)
102 {
103         String result;
104         for (InputIterator it = begin; it != end; ++it)
105         {
106                 if (!result.empty()) result.append(delim);
107                 result += func(*it);
108         }
109         return result;
110 }
111
112 inline String to_str(int val) { return strutils::format(_T("%d"), val); }
113 inline String to_str(unsigned val) { return strutils::format(_T("%u"), val); }
114 inline String to_str(long val) { return strutils::format(_T("%ld"), val); }
115 inline String to_str(unsigned long val) { return strutils::format(_T("%lu"), val); }
116 inline String to_str(long long val) { return strutils::format(_T("%I64d"), val); }
117 inline String to_str(unsigned long long val) { return strutils::format(_T("%I64u"), val); }
118 inline String to_str(float val) { return strutils::format(_T("%f"), val); }
119 inline String to_str(double val) { return strutils::format(_T("%f"), val); }
120
121 }