Files

147 lines
3.3 KiB
C++
Raw Permalink Normal View History

2000-09-26 11:48:28 +00:00
/*
*
* Copyright (c) 1998-2002
2005-01-21 17:22:39 +00:00
* John Maddock
2000-09-26 11:48:28 +00:00
*
2003-10-04 11:29:20 +00:00
* Use, modification and distribution are subject to the
2003-09-30 13:02:51 +00:00
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2000-09-26 11:48:28 +00:00
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE: primary_transform.hpp
* VERSION: see <boost/version.hpp>
2000-09-26 11:48:28 +00:00
* DESCRIPTION: Heuristically determines the sort string format in use
* by the current locale.
*/
2005-01-13 17:06:21 +00:00
#ifndef BOOST_REGEX_PRIMARY_TRANSFORM
#define BOOST_REGEX_PRIMARY_TRANSFORM
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable: 4103)
#endif
2005-01-13 17:06:21 +00:00
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
2005-01-13 17:06:21 +00:00
2000-09-26 11:48:28 +00:00
namespace boost{
2015-04-04 19:10:37 +01:00
namespace BOOST_REGEX_DETAIL_NS{
2000-09-26 11:48:28 +00:00
enum{
sort_C,
sort_fixed,
sort_delim,
sort_unknown
};
template <class S, class charT>
unsigned count_chars(const S& s, charT c)
{
2005-01-13 17:06:21 +00:00
//
2020-10-01 16:07:35 +02:00
// Count how many occurrences of character c occur
2005-01-13 17:06:21 +00:00
// in string s: if c is a delimeter between collation
// fields, then this should be the same value for all
// sort keys:
//
2000-09-26 11:48:28 +00:00
unsigned int count = 0;
2001-01-31 12:00:34 +00:00
for(unsigned pos = 0; pos < s.size(); ++pos)
2000-09-26 11:48:28 +00:00
{
if(s[pos] == c) ++count;
}
return count;
}
template <class traits, class charT>
unsigned find_sort_syntax(const traits* pt, charT* delim)
{
//
// compare 'a' with 'A' to see how similar they are,
// should really use a-accute but we can't portably do that,
//
typedef typename traits::string_type string_type;
typedef typename traits::char_type char_type;
// Suppress incorrect warning for MSVC
(void)pt;
2005-01-13 17:06:21 +00:00
char_type a[2] = {'a', '\0', };
string_type sa(pt->transform(a, a+1));
2000-09-26 11:48:28 +00:00
if(sa == a)
{
*delim = 0;
return sort_C;
}
2005-01-13 17:06:21 +00:00
char_type A[2] = { 'A', '\0', };
string_type sA(pt->transform(A, A+1));
char_type c[2] = { ';', '\0', };
string_type sc(pt->transform(c, c+1));
2000-09-26 11:48:28 +00:00
int pos = 0;
while((pos <= static_cast<int>(sa.size())) && (pos <= static_cast<int>(sA.size())) && (sa[pos] == sA[pos])) ++pos;
--pos;
if(pos < 0)
{
*delim = 0;
return sort_unknown;
}
//
2005-01-13 17:06:21 +00:00
// at this point sa[pos] is either the end of a fixed width field
2000-09-26 11:48:28 +00:00
// or the character that acts as a delimiter:
//
charT maybe_delim = sa[pos];
2005-01-13 17:06:21 +00:00
if((pos != 0) && (count_chars(sa, maybe_delim) == count_chars(sA, maybe_delim)) && (count_chars(sa, maybe_delim) == count_chars(sc, maybe_delim)))
2000-09-26 11:48:28 +00:00
{
*delim = maybe_delim;
return sort_delim;
}
//
// OK doen't look like a delimiter, try for fixed width field:
//
2005-01-13 17:06:21 +00:00
if((sa.size() == sA.size()) && (sa.size() == sc.size()))
2000-09-26 11:48:28 +00:00
{
// note assumes that the fixed width field is less than
2005-07-13 09:38:57 +00:00
// (numeric_limits<charT>::max)(), should be true for all types
2000-09-26 11:48:28 +00:00
// I can't imagine 127 character fields...
*delim = static_cast<charT>(++pos);
return sort_fixed;
}
//
// don't know what it is:
//
*delim = 0;
return sort_unknown;
}
2015-04-04 19:10:37 +01:00
} // namespace BOOST_REGEX_DETAIL_NS
2000-09-26 11:48:28 +00:00
} // namespace boost
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable: 4103)
#endif
2005-01-13 17:06:21 +00:00
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
2005-01-13 17:06:21 +00:00
#endif
2000-09-26 11:48:28 +00:00
2001-01-31 12:00:34 +00:00
2003-09-30 13:02:51 +00:00