~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/libs/regex/src/primary_transform.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright (c) 1998-2002
 
4
 * Dr John Maddock
 
5
 *
 
6
 * Permission to use, copy, modify, distribute and sell this software
 
7
 * and its documentation for any purpose is hereby granted without fee,
 
8
 * provided that the above copyright notice appear in all copies and
 
9
 * that both that copyright notice and this permission notice appear
 
10
 * in supporting documentation.  Dr John Maddock makes no representations
 
11
 * about the suitability of this software for any purpose.  
 
12
 * It is provided "as is" without express or implied warranty.
 
13
 *
 
14
 */
 
15
 
 
16
 /*
 
17
  *   LOCATION:    see http://www.boost.org for most recent version.
 
18
  *   FILE:        primary_transform.hpp
 
19
  *   VERSION:     see <boost/version.hpp>
 
20
  *   DESCRIPTION: Heuristically determines the sort string format in use
 
21
  *                by the current locale.
 
22
  */
 
23
 
 
24
namespace boost{
 
25
   namespace re_detail{
 
26
 
 
27
 
 
28
enum{
 
29
   sort_C,
 
30
   sort_fixed,
 
31
   sort_delim,
 
32
   sort_unknown
 
33
};
 
34
 
 
35
template <class S, class charT>
 
36
unsigned count_chars(const S& s, charT c)
 
37
{
 
38
   unsigned int count = 0;
 
39
   for(unsigned pos = 0; pos < s.size(); ++pos)
 
40
   {
 
41
      if(s[pos] == c) ++count;
 
42
   }
 
43
   return count;
 
44
}
 
45
 
 
46
 
 
47
template <class traits, class charT>
 
48
unsigned find_sort_syntax(const traits* pt, charT* delim)
 
49
{
 
50
   //
 
51
   // compare 'a' with 'A' to see how similar they are,
 
52
   // should really use a-accute but we can't portably do that,
 
53
   //
 
54
   typedef typename traits::string_type string_type;
 
55
   typedef typename traits::char_type char_type;
 
56
 
 
57
   // Suppress incorrect warning for MSVC
 
58
   (void)pt;
 
59
 
 
60
   string_type a(1, (char_type)'a');
 
61
   string_type sa;
 
62
   pt->transform(sa, a);
 
63
   if(sa == a)
 
64
   {
 
65
      *delim = 0;
 
66
      return sort_C;
 
67
   }
 
68
   string_type A(1, (char_type)'A');
 
69
   string_type sA;
 
70
   pt->transform(sA, A);
 
71
   string_type c(1, (char_type)';');
 
72
   string_type sc;
 
73
   pt->transform(sc, c);
 
74
 
 
75
   int pos = 0;
 
76
   while((pos <= static_cast<int>(sa.size())) && (pos <= static_cast<int>(sA.size())) && (sa[pos] == sA[pos])) ++pos;
 
77
   --pos;
 
78
   if(pos < 0)
 
79
   {
 
80
      *delim = 0;
 
81
      return sort_unknown;
 
82
   }
 
83
   //
 
84
   // at this point sa[pos] is either the end of a fixed with field
 
85
   // or the character that acts as a delimiter:
 
86
   //
 
87
   charT maybe_delim = sa[pos];
 
88
   if((pos != 0) && (count_chars(sa, maybe_delim) == count_chars(sA, maybe_delim)) && (count_chars(sa, maybe_delim) == count_chars(c, maybe_delim)))
 
89
   {
 
90
      *delim = maybe_delim;
 
91
      return sort_delim;
 
92
   }
 
93
   //
 
94
   // OK doen't look like a delimiter, try for fixed width field:
 
95
   //
 
96
   if((sa.size() == sA.size()) && (sa.size() == c.size()))
 
97
   {
 
98
      // note assumes that the fixed width field is less than
 
99
      // numeric_limits<charT>::max(), should be true for all types
 
100
      // I can't imagine 127 character fields...
 
101
      *delim = static_cast<charT>(++pos);
 
102
      return sort_fixed;
 
103
   }
 
104
   //
 
105
   // don't know what it is:
 
106
   //
 
107
   *delim = 0;
 
108
   return sort_unknown;
 
109
}
 
110
 
 
111
 
 
112
   } // namespace re_detail
 
113
} // namespace boost
 
114
 
 
115
 
 
116
 
 
117
 
 
118
 
 
119
 
 
120