source: NonGTP/Boost/boost/regex/v4/primary_transform.hpp @ 857

Revision 857, 3.1 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*
2 *
3 * Copyright (c) 1998-2002
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11 
12 /*
13  *   LOCATION:    see http://www.boost.org for most recent version.
14  *   FILE:        primary_transform.hpp
15  *   VERSION:     see <boost/version.hpp>
16  *   DESCRIPTION: Heuristically determines the sort string format in use
17  *                by the current locale.
18  */
19
20#ifndef BOOST_REGEX_PRIMARY_TRANSFORM
21#define BOOST_REGEX_PRIMARY_TRANSFORM
22
23#ifdef BOOST_HAS_ABI_HEADERS
24#  include BOOST_ABI_PREFIX
25#endif
26
27namespace boost{
28   namespace re_detail{
29
30
31enum{
32   sort_C,
33   sort_fixed,
34   sort_delim,
35   sort_unknown
36};
37
38template <class S, class charT>
39unsigned count_chars(const S& s, charT c)
40{
41   //
42   // Count how many occurances of character c occur
43   // in string s: if c is a delimeter between collation
44   // fields, then this should be the same value for all
45   // sort keys:
46   //
47   unsigned int count = 0;
48   for(unsigned pos = 0; pos < s.size(); ++pos)
49   {
50      if(s[pos] == c) ++count;
51   }
52   return count;
53}
54
55
56template <class traits, class charT>
57unsigned find_sort_syntax(const traits* pt, charT* delim)
58{
59   //
60   // compare 'a' with 'A' to see how similar they are,
61   // should really use a-accute but we can't portably do that,
62   //
63   typedef typename traits::string_type string_type;
64   typedef typename traits::char_type char_type;
65
66   // Suppress incorrect warning for MSVC
67   (void)pt;
68
69   char_type a[2] = {'a', '\0', };
70   string_type sa(pt->transform(a, a+1));
71   if(sa == a)
72   {
73      *delim = 0;
74      return sort_C;
75   }
76   char_type A[2] = { 'A', '\0', };
77   string_type sA(pt->transform(A, A+1));
78   char_type c[2] = { ';', '\0', };
79   string_type sc(pt->transform(c, c+1));
80
81   int pos = 0;
82   while((pos <= static_cast<int>(sa.size())) && (pos <= static_cast<int>(sA.size())) && (sa[pos] == sA[pos])) ++pos;
83   --pos;
84   if(pos < 0)
85   {
86      *delim = 0;
87      return sort_unknown;
88   }
89   //
90   // at this point sa[pos] is either the end of a fixed width field
91   // or the character that acts as a delimiter:
92   //
93   charT maybe_delim = sa[pos];
94   if((pos != 0) && (count_chars(sa, maybe_delim) == count_chars(sA, maybe_delim)) && (count_chars(sa, maybe_delim) == count_chars(sc, maybe_delim)))
95   {
96      *delim = maybe_delim;
97      return sort_delim;
98   }
99   //
100   // OK doen't look like a delimiter, try for fixed width field:
101   //
102   if((sa.size() == sA.size()) && (sa.size() == sc.size()))
103   {
104      // note assumes that the fixed width field is less than
105      // (numeric_limits<charT>::max)(), should be true for all types
106      // I can't imagine 127 character fields...
107      *delim = static_cast<charT>(++pos);
108      return sort_fixed;
109   }
110   //
111   // don't know what it is:
112   //
113   *delim = 0;
114   return sort_unknown;
115}
116
117
118   } // namespace re_detail
119} // namespace boost
120
121#ifdef BOOST_HAS_ABI_HEADERS
122#  include BOOST_ABI_SUFFIX
123#endif
124
125#endif
126
127
128
129
130
131
132
Note: See TracBrowser for help on using the repository browser.