1 // Character traits template for the -*- C++ -*- string classes.
2 // Copyright (C) 1994 Free Software Foundation
4 // This file is part of the GNU ANSI C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 2, or (at your option)
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING. If not, write to the Free
17 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // As a special exception, if you link this library with files
20 // compiled with a GNU compiler to produce an executable, this does not cause
21 // the resulting executable to be covered by the GNU General Public License.
22 // This exception does not however invalidate any other reasons why
23 // the executable file might be covered by the GNU General Public License.
25 // Written by Jason Merrill based upon the specification by Takanori Adachi
26 // in ANSI X3J16/94-0013R2.
28 #ifndef __STRING_CHAR_TRAITS__
29 #define __STRING_CHAR_TRAITS__
32 // For string_char_traits <char>
33 #pragma interface "std/straits.h"
39 template <class charT
>
40 struct string_char_traits
{
41 typedef charT char_type
; // for users to acquire the basic character type
45 static void assign (char_type
& c1
, const char_type
& c2
)
47 static bool eq (const char_type
& c1
, const char_type
& c2
)
48 { return (c1
== c2
); }
49 static bool ne (const char_type
& c1
, const char_type
& c2
)
50 { return !(c1
== c2
); }
51 static bool lt (const char_type
& c1
, const char_type
& c2
)
53 static char_type
eos () { return char_type(); } // the null character
54 static bool is_del(char_type a
) { return 0; }
55 // characteristic function for delimiters of charT
59 static int compare (const char_type
* s1
, const char_type
* s2
, size_t n
)
62 for (i
= 0; i
< n
; ++i
)
63 if (ne (s1
[i
], s2
[i
]))
64 return lt (s1
[i
], s2
[i
]) ? -1 : 1;
69 static size_t length (const char_type
* s
)
72 while (ne (*s
++, eos ()))
77 static char_type
* copy (char_type
* s1
, const char_type
* s2
, size_t n
)
80 assign (s1
[n
], s2
[n
]);
84 static char_type
* move (char_type
* s1
, const char_type
* s2
, size_t n
)
88 for (i
= 0; i
< n
; ++i
)
90 for (i
= 0; i
< n
; ++i
)
95 static char_type
* set (char_type
* s1
, const char_type
& c
, size_t n
)
108 struct string_char_traits
<char> {
109 typedef char char_type
;
111 static void assign (char_type
& c1
, const char_type
& c2
)
113 static bool eq (const char_type
& c1
, const char_type
& c2
)
114 { return (c1
== c2
); }
115 static bool ne (const char_type
& c1
, const char_type
& c2
)
116 { return (c1
!= c2
); }
117 static bool lt (const char_type
& c1
, const char_type
& c2
)
118 { return (c1
< c2
); }
119 static char_type
eos () { return 0; }
120 static bool is_del(char_type a
) { return isspace(a
); }
122 static int compare (const char_type
* s1
, const char_type
* s2
, size_t n
)
123 { return memcmp (s1
, s2
, n
); }
124 static size_t length (const char_type
* s
)
125 { return strlen (s
); }
126 static char_type
* copy (char_type
* s1
, const char_type
* s2
, size_t n
)
127 { return (char_type
*) memcpy (s1
, s2
, n
); }
128 static char_type
* move (char_type
* s1
, const char_type
* s2
, size_t n
)
129 { return (char_type
*) memmove (s1
, s2
, n
); }
130 static char_type
* set (char_type
* s1
, const char_type
& c
, size_t n
)
131 { return (char_type
*) memset (s1
, c
, n
); }
136 struct string_char_traits
<wchar_t> {
137 typedef wchar_t char_type
;
139 static void assign (char_type
& c1
, const char_type
& c2
)
141 static bool eq (const char_type
& c1
, const char_type
& c2
)
142 { return (c1
== c2
); }
143 static bool ne (const char_type
& c1
, const char_type
& c2
)
144 { return (c1
!= c2
); }
145 static bool lt (const char_type
& c1
, const char_type
& c2
)
146 { return (c1
< c2
); }
147 static char_type
eos () { return 0; }
148 static bool is_del(char_type a
) { return iswspace(a
); }
150 static int compare (const char_type
* s1
, const char_type
* s2
, size_t n
)
151 { return wmemcmp (s1
, s2
, n
); }
152 static size_t length (const char_type
* s
)
153 { return wcslen (s
); }
154 static char_type
* copy (char_type
* s1
, const char_type
* s2
, size_t n
)
155 { return wmemcpy (s1
, s2
, n
); }
156 static char_type
* set (char_type
* s1
, const char_type
& c
, size_t n
)
157 { return wmemset (s1
, c
, n
); }