xref: /haiku/headers/cpp/std/straits.h (revision f2ced752a08ff5d2618826bcd3ae3976c9f3e92e)
1*f2ced752SOliver Tappe // Character traits template for the -*- C++ -*- string classes.
2*f2ced752SOliver Tappe // Copyright (C) 1994 Free Software Foundation
3*f2ced752SOliver Tappe 
4*f2ced752SOliver Tappe // This file is part of the GNU ANSI C++ Library.  This library is free
5*f2ced752SOliver Tappe // software; you can redistribute it and/or modify it under the
6*f2ced752SOliver Tappe // terms of the GNU General Public License as published by the
7*f2ced752SOliver Tappe // Free Software Foundation; either version 2, or (at your option)
8*f2ced752SOliver Tappe // any later version.
9*f2ced752SOliver Tappe 
10*f2ced752SOliver Tappe // This library is distributed in the hope that it will be useful,
11*f2ced752SOliver Tappe // but WITHOUT ANY WARRANTY; without even the implied warranty of
12*f2ced752SOliver Tappe // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*f2ced752SOliver Tappe // GNU General Public License for more details.
14*f2ced752SOliver Tappe 
15*f2ced752SOliver Tappe // You should have received a copy of the GNU General Public License
16*f2ced752SOliver Tappe // along with this library; see the file COPYING.  If not, write to the Free
17*f2ced752SOliver Tappe // Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*f2ced752SOliver Tappe 
19*f2ced752SOliver Tappe // As a special exception, if you link this library with files
20*f2ced752SOliver Tappe // compiled with a GNU compiler to produce an executable, this does not cause
21*f2ced752SOliver Tappe // the resulting executable to be covered by the GNU General Public License.
22*f2ced752SOliver Tappe // This exception does not however invalidate any other reasons why
23*f2ced752SOliver Tappe // the executable file might be covered by the GNU General Public License.
24*f2ced752SOliver Tappe 
25*f2ced752SOliver Tappe // Written by Jason Merrill based upon the specification by Takanori Adachi
26*f2ced752SOliver Tappe // in ANSI X3J16/94-0013R2.
27*f2ced752SOliver Tappe 
28*f2ced752SOliver Tappe #ifndef __STRING_CHAR_TRAITS__
29*f2ced752SOliver Tappe #define __STRING_CHAR_TRAITS__
30*f2ced752SOliver Tappe 
31*f2ced752SOliver Tappe #ifdef __GNUG__
32*f2ced752SOliver Tappe // For string_char_traits <char>
33*f2ced752SOliver Tappe #pragma interface "std/straits.h"
34*f2ced752SOliver Tappe #endif
35*f2ced752SOliver Tappe 
36*f2ced752SOliver Tappe #include <cstddef>
37*f2ced752SOliver Tappe 
38*f2ced752SOliver Tappe extern "C++" {
39*f2ced752SOliver Tappe template <class charT>
40*f2ced752SOliver Tappe struct string_char_traits {
41*f2ced752SOliver Tappe   typedef charT char_type; // for users to acquire the basic character type
42*f2ced752SOliver Tappe 
43*f2ced752SOliver Tappe   // constraints
44*f2ced752SOliver Tappe 
assignstring_char_traits45*f2ced752SOliver Tappe   static void assign (char_type& c1, const char_type& c2)
46*f2ced752SOliver Tappe     { c1 = c2; }
eqstring_char_traits47*f2ced752SOliver Tappe   static bool eq (const char_type& c1, const char_type& c2)
48*f2ced752SOliver Tappe     { return (c1 == c2); }
nestring_char_traits49*f2ced752SOliver Tappe   static bool ne (const char_type& c1, const char_type& c2)
50*f2ced752SOliver Tappe     { return !(c1 == c2); }
ltstring_char_traits51*f2ced752SOliver Tappe   static bool lt (const char_type& c1, const char_type& c2)
52*f2ced752SOliver Tappe     { return (c1 < c2); }
eosstring_char_traits53*f2ced752SOliver Tappe   static char_type eos () { return char_type(); } // the null character
is_delstring_char_traits54*f2ced752SOliver Tappe   static bool is_del(char_type a) { return 0; }
55*f2ced752SOliver Tappe   // characteristic function for delimiters of charT
56*f2ced752SOliver Tappe 
57*f2ced752SOliver Tappe   // speed-up functions
58*f2ced752SOliver Tappe 
comparestring_char_traits59*f2ced752SOliver Tappe   static int compare (const char_type* s1, const char_type* s2, size_t n)
60*f2ced752SOliver Tappe     {
61*f2ced752SOliver Tappe       size_t i;
62*f2ced752SOliver Tappe       for (i = 0; i < n; ++i)
63*f2ced752SOliver Tappe 	if (ne (s1[i], s2[i]))
64*f2ced752SOliver Tappe 	  return lt (s1[i], s2[i]) ? -1 : 1;
65*f2ced752SOliver Tappe 
66*f2ced752SOliver Tappe       return 0;
67*f2ced752SOliver Tappe     }
68*f2ced752SOliver Tappe 
lengthstring_char_traits69*f2ced752SOliver Tappe   static size_t length (const char_type* s)
70*f2ced752SOliver Tappe     {
71*f2ced752SOliver Tappe       size_t l = 0;
72*f2ced752SOliver Tappe       while (ne (*s++, eos ()))
73*f2ced752SOliver Tappe 	++l;
74*f2ced752SOliver Tappe       return l;
75*f2ced752SOliver Tappe     }
76*f2ced752SOliver Tappe 
copystring_char_traits77*f2ced752SOliver Tappe   static char_type* copy (char_type* s1, const char_type* s2, size_t n)
78*f2ced752SOliver Tappe     {
79*f2ced752SOliver Tappe       for (; n--; )
80*f2ced752SOliver Tappe 	assign (s1[n], s2[n]);
81*f2ced752SOliver Tappe       return s1;
82*f2ced752SOliver Tappe     }
83*f2ced752SOliver Tappe 
movestring_char_traits84*f2ced752SOliver Tappe   static char_type* move (char_type* s1, const char_type* s2, size_t n)
85*f2ced752SOliver Tappe     {
86*f2ced752SOliver Tappe       char_type a[n];
87*f2ced752SOliver Tappe       size_t i;
88*f2ced752SOliver Tappe       for (i = 0; i < n; ++i)
89*f2ced752SOliver Tappe 	assign (a[i], s2[i]);
90*f2ced752SOliver Tappe       for (i = 0; i < n; ++i)
91*f2ced752SOliver Tappe 	assign (s1[i], a[i]);
92*f2ced752SOliver Tappe       return s1;
93*f2ced752SOliver Tappe     }
94*f2ced752SOliver Tappe 
setstring_char_traits95*f2ced752SOliver Tappe   static char_type* set (char_type* s1, const char_type& c, size_t n)
96*f2ced752SOliver Tappe     {
97*f2ced752SOliver Tappe       for (; n--; )
98*f2ced752SOliver Tappe 	assign (s1[n], c);
99*f2ced752SOliver Tappe       return s1;
100*f2ced752SOliver Tappe     }
101*f2ced752SOliver Tappe };
102*f2ced752SOliver Tappe 
103*f2ced752SOliver Tappe class istream;
104*f2ced752SOliver Tappe class ostream;
105*f2ced752SOliver Tappe #include <cctype>
106*f2ced752SOliver Tappe #include <cstring>
107*f2ced752SOliver Tappe 
108*f2ced752SOliver Tappe struct string_char_traits <char> {
109*f2ced752SOliver Tappe   typedef char char_type;
110*f2ced752SOliver Tappe 
111*f2ced752SOliver Tappe   static void assign (char_type& c1, const char_type& c2)
112*f2ced752SOliver Tappe     { c1 = c2; }
113*f2ced752SOliver Tappe   static bool eq (const char_type & c1, const char_type& c2)
114*f2ced752SOliver Tappe     { return (c1 == c2); }
115*f2ced752SOliver Tappe   static bool ne (const char_type& c1, const char_type& c2)
116*f2ced752SOliver Tappe     { return (c1 != c2); }
117*f2ced752SOliver Tappe   static bool lt (const char_type& c1, const char_type& c2)
118*f2ced752SOliver Tappe     { return (c1 < c2); }
119*f2ced752SOliver Tappe   static char_type eos () { return 0; }
120*f2ced752SOliver Tappe   static bool is_del(char_type a) { return isspace(a); }
121*f2ced752SOliver Tappe 
122*f2ced752SOliver Tappe   static int compare (const char_type* s1, const char_type* s2, size_t n)
123*f2ced752SOliver Tappe     { return memcmp (s1, s2, n); }
124*f2ced752SOliver Tappe   static size_t length (const char_type* s)
125*f2ced752SOliver Tappe     { return strlen (s); }
126*f2ced752SOliver Tappe   static char_type* copy (char_type* s1, const char_type* s2, size_t n)
127*f2ced752SOliver Tappe     { return (char_type*) memcpy (s1, s2, n); }
128*f2ced752SOliver Tappe   static char_type* move (char_type* s1, const char_type* s2, size_t n)
129*f2ced752SOliver Tappe     { return (char_type*) memmove (s1, s2, n); }
130*f2ced752SOliver Tappe   static char_type* set (char_type* s1, const char_type& c, size_t n)
131*f2ced752SOliver Tappe     { return (char_type*) memset (s1, c, n); }
132*f2ced752SOliver Tappe };
133*f2ced752SOliver Tappe 
134*f2ced752SOliver Tappe #if 0
135*f2ced752SOliver Tappe #include <cwctype>
136*f2ced752SOliver Tappe struct string_char_traits <wchar_t> {
137*f2ced752SOliver Tappe   typedef wchar_t char_type;
138*f2ced752SOliver Tappe 
139*f2ced752SOliver Tappe   static void assign (char_type& c1, const char_type& c2)
140*f2ced752SOliver Tappe     { c1 = c2; }
141*f2ced752SOliver Tappe   static bool eq (const char_type & c1, const char_type& c2)
142*f2ced752SOliver Tappe     { return (c1 == c2); }
143*f2ced752SOliver Tappe   static bool ne (const char_type& c1, const char_type& c2)
144*f2ced752SOliver Tappe     { return (c1 != c2); }
145*f2ced752SOliver Tappe   static bool lt (const char_type& c1, const char_type& c2)
146*f2ced752SOliver Tappe     { return (c1 < c2); }
147*f2ced752SOliver Tappe   static char_type eos () { return 0; }
148*f2ced752SOliver Tappe   static bool is_del(char_type a) { return iswspace(a); }
149*f2ced752SOliver Tappe 
150*f2ced752SOliver Tappe   static int compare (const char_type* s1, const char_type* s2, size_t n)
151*f2ced752SOliver Tappe     { return wmemcmp (s1, s2, n); }
152*f2ced752SOliver Tappe   static size_t length (const char_type* s)
153*f2ced752SOliver Tappe     { return wcslen (s); }
154*f2ced752SOliver Tappe   static char_type* copy (char_type* s1, const char_type* s2, size_t n)
155*f2ced752SOliver Tappe     { return wmemcpy (s1, s2, n); }
156*f2ced752SOliver Tappe   static char_type* set (char_type* s1, const char_type& c, size_t n)
157*f2ced752SOliver Tappe     { return wmemset (s1, c, n); }
158*f2ced752SOliver Tappe };
159*f2ced752SOliver Tappe #endif
160*f2ced752SOliver Tappe } // extern "C++"
161*f2ced752SOliver Tappe #endif
162