Dimitry Andric d72607e9e2 Import libc++ trunk r224926. This fixes a number of bugs, completes
C++14 support[1], adds more C++1z features[2], and fixes the following
LWG issues[3]:

1450: Contradiction in regex_constants
2003: String exception inconsistency in erase.
2075: Progress guarantees, lock-free property, and scheduling
      assumptions
2104: unique_lock move-assignment should not be noexcept
2112: User-defined classes that cannot be derived from
2132: std::function ambiguity
2135: Unclear requirement for exceptions thrown in
      condition_variable::wait()
2142: packaged_task::operator() synchronization too broad?
2182: Container::[const_]reference types are misleadingly specified
2186: Incomplete action on async/launch::deferred
2188: Reverse iterator does not fully support targets that overload
      operator&
2193: Default constructors for standard library containers are explicit
2205: Problematic postconditions of regex_match and regex_search
2213: Return value of std::regex_replace
2240: Probable misuse of term "function scope" in [thread.condition]
2252: Strong guarantee on vector::push_back() still broken with C++11?
2257: Simplify container requirements with the new algorithms
2258: a.erase(q1, q2) unable to directly return q2
2263: Comparing iterators and allocator pointers with different
      const-character
2268: Setting a default argument in the declaration of a member
      function assign of std::basic_string
2271: regex_traits::lookup_classname specification unclear
2272: quoted should use char_traits::eq for character comparison
2278: User-defined literals for Standard Library types
2280: begin / end for arrays should be constexpr and noexcept
2285: make_reverse_iterator
2288: Inconsistent requirements for shared mutexes
2291: std::hash is vulnerable to collision DoS attack
2293: Wrong facet used by num_put::do_put
2299: Effects of inaccessible key_compare::is_transparent type are not
      clear
2301: Why is std::tie not constexpr?
2304: Complexity of count in unordered associative containers
2306: match_results::reference should be value_type&, not const
      value_type&
2308: Clarify container destructor requirements w.r.t. std::array
2313: tuple_size should always derive from integral_constant<size_t, N>
2314: apply() should return decltype(auto) and use decay_t before
      tuple_size
2315: weak_ptr should be movable
2316: weak_ptr::lock() should be atomic
2317: The type property queries should be UnaryTypeTraits returning
      size_t
2320: select_on_container_copy_construction() takes allocators, not
      containers
2322: Associative(initializer_list, stuff) constructors are
      underspecified
2323: vector::resize(n, t)'s specification should be simplified
2324: Insert iterator constructors should use addressof()
2329: regex_match()/regex_search() with match_results should forbid
      temporary strings
2330: regex("meow", regex::icase) is technically forbidden but should
      be permitted
2332: regex_iterator/regex_token_iterator should forbid temporary
      regexes
2339: Wording issue in nth_element
2341: Inconsistency between basic_ostream::seekp(pos) and
      basic_ostream::seekp(off, dir)
2344: quoted()'s interaction with padding is unclear
2346: integral_constant's member functions should be marked noexcept
2350: min, max, and minmax should be constexpr
2356: Stability of erasure in unordered associative containers
2357: Remaining "Assignable" requirement
2359: How does regex_constants::nosubs affect basic_regex::mark_count()?
2360: reverse_iterator::operator*() is unimplementable

[1] http://libcxx.llvm.org/cxx1y_status.html
[2] http://libcxx.llvm.org/cxx1z_status.html
[3] http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html

Exp-run:	antoine
MFC after:	1 month
2015-01-15 21:17:36 +00:00

334 lines
8.4 KiB
C++

//===-------------------------- regex.cpp ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "regex"
#include "algorithm"
#include "iterator"
_LIBCPP_BEGIN_NAMESPACE_STD
static
const char*
make_error_type_string(regex_constants::error_type ecode)
{
switch (ecode)
{
case regex_constants::error_collate:
return "The expression contained an invalid collating element name.";
case regex_constants::error_ctype:
return "The expression contained an invalid character class name.";
case regex_constants::error_escape:
return "The expression contained an invalid escaped character, or a "
"trailing escape.";
case regex_constants::error_backref:
return "The expression contained an invalid back reference.";
case regex_constants::error_brack:
return "The expression contained mismatched [ and ].";
case regex_constants::error_paren:
return "The expression contained mismatched ( and ).";
case regex_constants::error_brace:
return "The expression contained mismatched { and }.";
case regex_constants::error_badbrace:
return "The expression contained an invalid range in a {} expression.";
case regex_constants::error_range:
return "The expression contained an invalid character range, "
"such as [b-a] in most encodings.";
case regex_constants::error_space:
return "There was insufficient memory to convert the expression into "
"a finite state machine.";
case regex_constants::error_badrepeat:
return "One of *?+{ was not preceded by a valid regular expression.";
case regex_constants::error_complexity:
return "The complexity of an attempted match against a regular "
"expression exceeded a pre-set level.";
case regex_constants::error_stack:
return "There was insufficient memory to determine whether the regular "
"expression could match the specified character sequence.";
case regex_constants::__re_err_grammar:
return "An invalid regex grammar has been requested.";
case regex_constants::__re_err_empty:
return "An empty regex is not allowed in the POSIX grammar.";
default:
break;
}
return "Unknown error type";
}
regex_error::regex_error(regex_constants::error_type ecode)
: runtime_error(make_error_type_string(ecode)),
__code_(ecode)
{}
regex_error::~regex_error() throw() {}
namespace {
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
#endif
struct collationnames
{
const char* elem_;
char char_;
};
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
const collationnames collatenames[] =
{
{"A", 0x41},
{"B", 0x42},
{"C", 0x43},
{"D", 0x44},
{"E", 0x45},
{"F", 0x46},
{"G", 0x47},
{"H", 0x48},
{"I", 0x49},
{"J", 0x4a},
{"K", 0x4b},
{"L", 0x4c},
{"M", 0x4d},
{"N", 0x4e},
{"NUL", 0x00},
{"O", 0x4f},
{"P", 0x50},
{"Q", 0x51},
{"R", 0x52},
{"S", 0x53},
{"T", 0x54},
{"U", 0x55},
{"V", 0x56},
{"W", 0x57},
{"X", 0x58},
{"Y", 0x59},
{"Z", 0x5a},
{"a", 0x61},
{"alert", 0x07},
{"ampersand", 0x26},
{"apostrophe", 0x27},
{"asterisk", 0x2a},
{"b", 0x62},
{"backslash", 0x5c},
{"backspace", 0x08},
{"c", 0x63},
{"carriage-return", 0x0d},
{"circumflex", 0x5e},
{"circumflex-accent", 0x5e},
{"colon", 0x3a},
{"comma", 0x2c},
{"commercial-at", 0x40},
{"d", 0x64},
{"dollar-sign", 0x24},
{"e", 0x65},
{"eight", 0x38},
{"equals-sign", 0x3d},
{"exclamation-mark", 0x21},
{"f", 0x66},
{"five", 0x35},
{"form-feed", 0x0c},
{"four", 0x34},
{"full-stop", 0x2e},
{"g", 0x67},
{"grave-accent", 0x60},
{"greater-than-sign", 0x3e},
{"h", 0x68},
{"hyphen", 0x2d},
{"hyphen-minus", 0x2d},
{"i", 0x69},
{"j", 0x6a},
{"k", 0x6b},
{"l", 0x6c},
{"left-brace", 0x7b},
{"left-curly-bracket", 0x7b},
{"left-parenthesis", 0x28},
{"left-square-bracket", 0x5b},
{"less-than-sign", 0x3c},
{"low-line", 0x5f},
{"m", 0x6d},
{"n", 0x6e},
{"newline", 0x0a},
{"nine", 0x39},
{"number-sign", 0x23},
{"o", 0x6f},
{"one", 0x31},
{"p", 0x70},
{"percent-sign", 0x25},
{"period", 0x2e},
{"plus-sign", 0x2b},
{"q", 0x71},
{"question-mark", 0x3f},
{"quotation-mark", 0x22},
{"r", 0x72},
{"reverse-solidus", 0x5c},
{"right-brace", 0x7d},
{"right-curly-bracket", 0x7d},
{"right-parenthesis", 0x29},
{"right-square-bracket", 0x5d},
{"s", 0x73},
{"semicolon", 0x3b},
{"seven", 0x37},
{"six", 0x36},
{"slash", 0x2f},
{"solidus", 0x2f},
{"space", 0x20},
{"t", 0x74},
{"tab", 0x09},
{"three", 0x33},
{"tilde", 0x7e},
{"two", 0x32},
{"u", 0x75},
{"underscore", 0x5f},
{"v", 0x76},
{"vertical-line", 0x7c},
{"vertical-tab", 0x0b},
{"w", 0x77},
{"x", 0x78},
{"y", 0x79},
{"z", 0x7a},
{"zero", 0x30}
};
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
#endif
struct classnames
{
const char* elem_;
regex_traits<char>::char_class_type mask_;
};
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
const classnames ClassNames[] =
{
{"alnum", ctype_base::alnum},
{"alpha", ctype_base::alpha},
{"blank", ctype_base::blank},
{"cntrl", ctype_base::cntrl},
{"d", ctype_base::digit},
{"digit", ctype_base::digit},
{"graph", ctype_base::graph},
{"lower", ctype_base::lower},
{"print", ctype_base::print},
{"punct", ctype_base::punct},
{"s", ctype_base::space},
{"space", ctype_base::space},
{"upper", ctype_base::upper},
{"w", regex_traits<char>::__regex_word},
{"xdigit", ctype_base::xdigit}
};
struct use_strcmp
{
bool operator()(const collationnames& x, const char* y)
{return strcmp(x.elem_, y) < 0;}
bool operator()(const classnames& x, const char* y)
{return strcmp(x.elem_, y) < 0;}
};
}
string
__get_collation_name(const char* s)
{
const collationnames* i =
_VSTD::lower_bound(begin(collatenames), end(collatenames), s, use_strcmp());
string r;
if (i != end(collatenames) && strcmp(s, i->elem_) == 0)
r = char(i->char_);
return r;
}
regex_traits<char>::char_class_type
__get_classname(const char* s, bool __icase)
{
const classnames* i =
_VSTD::lower_bound(begin(ClassNames), end(ClassNames), s, use_strcmp());
regex_traits<char>::char_class_type r = 0;
if (i != end(ClassNames) && strcmp(s, i->elem_) == 0)
{
r = i->mask_;
if (r == regex_traits<char>::__regex_word)
r |= ctype_base::alnum | ctype_base::upper | ctype_base::lower;
else if (__icase)
{
if (r & (ctype_base::lower | ctype_base::upper))
r |= ctype_base::alpha;
}
}
return r;
}
template <>
void
__match_any_but_newline<char>::__exec(__state& __s) const
{
if (__s.__current_ != __s.__last_)
{
switch (*__s.__current_)
{
case '\r':
case '\n':
__s.__do_ = __state::__reject;
__s.__node_ = nullptr;
break;
default:
__s.__do_ = __state::__accept_and_consume;
++__s.__current_;
__s.__node_ = this->first();
break;
}
}
else
{
__s.__do_ = __state::__reject;
__s.__node_ = nullptr;
}
}
template <>
void
__match_any_but_newline<wchar_t>::__exec(__state& __s) const
{
if (__s.__current_ != __s.__last_)
{
switch (*__s.__current_)
{
case '\r':
case '\n':
case 0x2028:
case 0x2029:
__s.__do_ = __state::__reject;
__s.__node_ = nullptr;
break;
default:
__s.__do_ = __state::__accept_and_consume;
++__s.__current_;
__s.__node_ = this->first();
break;
}
}
else
{
__s.__do_ = __state::__reject;
__s.__node_ = nullptr;
}
}
_LIBCPP_END_NAMESPACE_STD