Just getting our toes wet on <regex>

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@106187 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant
2010-06-17 00:34:59 +00:00
parent 4b3a0887d0
commit 3257c9853f
32 changed files with 2775 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// regex_traits();
#include <regex>
int main()
{
std::regex_traits<char> t1();
std::regex_traits<wchar_t> t2();
}

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// locale_type getloc()const;
#include <regex>
int main()
{
#error getloc not implemented
}

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// locale_type imbue(locale_type l);
#include <regex>
int main()
{
#error imbue not implemented
}

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// bool isctype(charT c, char_class_type f) const;
#include <regex>
int main()
{
#error isctype not implemented
}

View File

@@ -0,0 +1,251 @@
#ifndef ITERATORS_H
#define ITERATORS_H
#include <iterator>
template <class It>
class input_iterator
{
It it_;
template <class U> friend class input_iterator;
public:
typedef std::input_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
input_iterator() : it_() {}
explicit input_iterator(It it) : it_(it) {}
template <class U>
input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
input_iterator& operator++() {++it_; return *this;}
input_iterator operator++(int)
{input_iterator tmp(*this); ++(*this); return tmp;}
friend bool operator==(const input_iterator& x, const input_iterator& y)
{return x.it_ == y.it_;}
friend bool operator!=(const input_iterator& x, const input_iterator& y)
{return !(x == y);}
};
template <class T, class U>
inline
bool
operator==(const input_iterator<T>& x, const input_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool
operator!=(const input_iterator<T>& x, const input_iterator<U>& y)
{
return !(x == y);
}
template <class It>
class forward_iterator
{
It it_;
template <class U> friend class forward_iterator;
public:
typedef std::forward_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
forward_iterator() : it_() {}
explicit forward_iterator(It it) : it_(it) {}
template <class U>
forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
forward_iterator& operator++() {++it_; return *this;}
forward_iterator operator++(int)
{forward_iterator tmp(*this); ++(*this); return tmp;}
friend bool operator==(const forward_iterator& x, const forward_iterator& y)
{return x.it_ == y.it_;}
friend bool operator!=(const forward_iterator& x, const forward_iterator& y)
{return !(x == y);}
};
template <class T, class U>
inline
bool
operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool
operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
{
return !(x == y);
}
template <class It>
class bidirectional_iterator
{
It it_;
template <class U> friend class bidirectional_iterator;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
bidirectional_iterator() : it_() {}
explicit bidirectional_iterator(It it) : it_(it) {}
template <class U>
bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
bidirectional_iterator& operator++() {++it_; return *this;}
bidirectional_iterator operator++(int)
{bidirectional_iterator tmp(*this); ++(*this); return tmp;}
bidirectional_iterator& operator--() {--it_; return *this;}
bidirectional_iterator operator--(int)
{bidirectional_iterator tmp(*this); --(*this); return tmp;}
};
template <class T, class U>
inline
bool
operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool
operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
{
return !(x == y);
}
template <class It>
class random_access_iterator
{
It it_;
template <class U> friend class random_access_iterator;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
It base() const {return it_;}
random_access_iterator() : it_() {}
explicit random_access_iterator(It it) : it_(it) {}
template <class U>
random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
reference operator*() const {return *it_;}
pointer operator->() const {return it_;}
random_access_iterator& operator++() {++it_; return *this;}
random_access_iterator operator++(int)
{random_access_iterator tmp(*this); ++(*this); return tmp;}
random_access_iterator& operator--() {--it_; return *this;}
random_access_iterator operator--(int)
{random_access_iterator tmp(*this); --(*this); return tmp;}
random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
random_access_iterator operator+(difference_type n) const
{random_access_iterator tmp(*this); tmp += n; return tmp;}
friend random_access_iterator operator+(difference_type n, random_access_iterator x)
{x += n; return x;}
random_access_iterator& operator-=(difference_type n) {return *this += -n;}
random_access_iterator operator-(difference_type n) const
{random_access_iterator tmp(*this); tmp -= n; return tmp;}
reference operator[](difference_type n) const {return it_[n];}
};
template <class T, class U>
inline
bool
operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool
operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(x == y);
}
template <class T, class U>
inline
bool
operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() < y.base();
}
template <class T, class U>
inline
bool
operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(y < x);
}
template <class T, class U>
inline
bool
operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return y < x;
}
template <class T, class U>
inline
bool
operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(x < y);
}
template <class T, class U>
inline
typename std::iterator_traits<T>::difference_type
operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() - y.base();
}
#endif

View File

@@ -0,0 +1,31 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// static std::size_t length(const char_type* p);
#include <regex>
#include <cassert>
int main()
{
assert(std::regex_traits<char>::length("") == 0);
assert(std::regex_traits<char>::length("1") == 1);
assert(std::regex_traits<char>::length("12") == 2);
assert(std::regex_traits<char>::length("123") == 3);
assert(std::regex_traits<wchar_t>::length(L"") == 0);
assert(std::regex_traits<wchar_t>::length(L"1") == 1);
assert(std::regex_traits<wchar_t>::length(L"12") == 2);
assert(std::regex_traits<wchar_t>::length(L"123") == 3);
}

View File

@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// template <class ForwardIterator>
// char_class_type
// lookup_classname(ForwardIterator first, ForwardIterator last,
// bool icase = false) const;
#include <regex>
int main()
{
#error lookup_classname not implemented
}

View File

@@ -0,0 +1,187 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// template <class ForwardIterator>
// string_type
// lookup_collatename(ForwardIterator first, ForwardIterator last) const;
#include <iostream>
#include <regex>
#include <iterator>
#include <cassert>
#include "iterators.h"
template <class char_type>
void
test(const char_type* A, const char_type* expected)
{
std::regex_traits<char_type> t;
typedef forward_iterator<const char_type*> F;
assert(t.lookup_collatename(F(A), F(A + t.length(A))) == expected);
}
int main()
{
test("NUL", "\x00");
test("alert", "\x07");
test("backspace", "\x08");
test("tab", "\x09");
test("carriage-return", "\x0D");
test("newline", "\x0A");
test("vertical-tab", "\x0B");
test("form-feed", "\x0C");
test("space", " ");
test("exclamation-mark", "!");
test("quotation-mark", "\"");
test("number-sign", "#");
test("dollar-sign", "$");
test("percent-sign", "%");
test("ampersand", "&");
test("apostrophe", "\'");
test("left-parenthesis", "(");
test("right-parenthesis", ")");
test("asterisk", "*");
test("plus-sign", "+");
test("comma", ",");
test("hyphen-minus", "-");
test("hyphen", "-");
test("full-stop", ".");
test("period", ".");
test("slash", "/");
test("solidus", "/");
test("zero", "0");
test("one", "1");
test("two", "2");
test("three", "3");
test("four", "4");
test("five", "5");
test("six", "6");
test("seven", "7");
test("eight", "8");
test("nine", "9");
test("colon", ":");
test("semicolon", ";");
test("less-than-sign", "<");
test("equals-sign", "=");
test("greater-than-sign", ">");
test("question-mark", "?");
test("commercial-at", "@");
for (char c = 'A'; c <= 'Z'; ++c)
{
const char a[2] = {c};
test(a, a);
}
test("left-square-bracket", "[");
test("backslash", "\\");
test("reverse-solidus", "\\");
test("right-square-bracket", "]");
test("circumflex-accent", "^");
test("circumflex", "^");
test("low-line", "_");
test("underscore", "_");
test("grave-accent", "`");
for (char c = 'a'; c <= 'z'; ++c)
{
const char a[2] = {c};
test(a, a);
}
test("left-brace", "{");
test("left-curly-bracket", "{");
test("vertical-line", "|");
test("right-brace", "}");
test("right-curly-bracket", "}");
test("tilde", "~");
test("tild", "");
test("ch", "");
std::locale::global(std::locale("cs_CZ.ISO8859-2"));
test("ch", "ch");
std::locale::global(std::locale("C"));
test(L"NUL", L"\x00");
test(L"alert", L"\x07");
test(L"backspace", L"\x08");
test(L"tab", L"\x09");
test(L"carriage-return", L"\x0D");
test(L"newline", L"\x0A");
test(L"vertical-tab", L"\x0B");
test(L"form-feed", L"\x0C");
test(L"space", L" ");
test(L"exclamation-mark", L"!");
test(L"quotation-mark", L"\"");
test(L"number-sign", L"#");
test(L"dollar-sign", L"$");
test(L"percent-sign", L"%");
test(L"ampersand", L"&");
test(L"apostrophe", L"\'");
test(L"left-parenthesis", L"(");
test(L"right-parenthesis", L")");
test(L"asterisk", L"*");
test(L"plus-sign", L"+");
test(L"comma", L",");
test(L"hyphen-minus", L"-");
test(L"hyphen", L"-");
test(L"full-stop", L".");
test(L"period", L".");
test(L"slash", L"/");
test(L"solidus", L"/");
test(L"zero", L"0");
test(L"one", L"1");
test(L"two", L"2");
test(L"three", L"3");
test(L"four", L"4");
test(L"five", L"5");
test(L"six", L"6");
test(L"seven", L"7");
test(L"eight", L"8");
test(L"nine", L"9");
test(L"colon", L":");
test(L"semicolon", L";");
test(L"less-than-sign", L"<");
test(L"equals-sign", L"=");
test(L"greater-than-sign", L">");
test(L"question-mark", L"?");
test(L"commercial-at", L"@");
for (wchar_t c = L'A'; c <= L'Z'; ++c)
{
const wchar_t a[2] = {c};
test(a, a);
}
test(L"left-square-bracket", L"[");
test(L"backslash", L"\\");
test(L"reverse-solidus", L"\\");
test(L"right-square-bracket", L"]");
test(L"circumflex-accent", L"^");
test(L"circumflex", L"^");
test(L"low-line", L"_");
test(L"underscore", L"_");
test(L"grave-accent", L"`");
for (wchar_t c = L'a'; c <= L'z'; ++c)
{
const wchar_t a[2] = {c};
test(a, a);
}
test(L"left-brace", L"{");
test(L"left-curly-bracket", L"{");
test(L"vertical-line", L"|");
test(L"right-brace", L"}");
test(L"right-curly-bracket", L"}");
test(L"tilde", L"~");
test(L"tild", L"");
test(L"ch", L"");
std::locale::global(std::locale("cs_CZ.ISO8859-2"));
test(L"ch", L"ch");
std::locale::global(std::locale("C"));
}

View File

@@ -0,0 +1,42 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// template <class ForwardIterator>
// string_type transform(ForwardIterator first, ForwardIterator last) const;
#include <regex>
#include <cassert>
#include "iterators.h"
int main()
{
{
std::regex_traits<char> t;
const char a[] = "a";
const char B[] = "B";
typedef forward_iterator<const char*> F;
assert(t.transform(F(a), F(a+1)) > t.transform(F(B), F(B+1)));
t.imbue(std::locale("cs_CZ.ISO8859-2"));
assert(t.transform(F(a), F(a+1)) < t.transform(F(B), F(B+1)));
}
{
std::regex_traits<wchar_t> t;
const wchar_t a[] = L"a";
const wchar_t B[] = L"B";
typedef forward_iterator<const wchar_t*> F;
assert(t.transform(F(a), F(a+1)) > t.transform(F(B), F(B+1)));
t.imbue(std::locale("cs_CZ.ISO8859-2"));
assert(t.transform(F(a), F(a+1)) < t.transform(F(B), F(B+1)));
}
}

View File

@@ -0,0 +1,49 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// template <class ForwardIterator>
// string_type
// transform_primary(ForwardIterator first, ForwardIterator last) const;
#include <iostream>
#include <regex>
#include <cassert>
#include "iterators.h"
int main()
{
{
std::regex_traits<char> t;
const char A[] = "A";
const char Aacute[] = "\xC1";
typedef forward_iterator<const char*> F;
assert(t.transform_primary(F(A), F(A+1)) !=
t.transform_primary(F(Aacute), F(Aacute+1)));
t.imbue(std::locale("cs_CZ.ISO8859-2"));
assert(t.transform_primary(F(A), F(A+1)) ==
t.transform_primary(F(Aacute), F(Aacute+1)));
}
{
std::regex_traits<wchar_t> t;
const wchar_t A[] = L"A";
const wchar_t Aacute[] = L"\xC1";
typedef forward_iterator<const wchar_t*> F;
assert(t.transform_primary(F(A), F(A+1)) !=
t.transform_primary(F(Aacute), F(Aacute+1)));
t.imbue(std::locale("cs_CZ.ISO8859-2"));
assert(t.transform_primary(F(A), F(A+1)) ==
t.transform_primary(F(Aacute), F(Aacute+1)));
}
}

View File

@@ -0,0 +1,34 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// charT translate(charT c) const;
#include <regex>
#include <cassert>
int main()
{
{
std::regex_traits<char> t;
assert(t.translate('a') == 'a');
assert(t.translate('B') == 'B');
assert(t.translate('c') == 'c');
}
{
std::regex_traits<wchar_t> t;
assert(t.translate(L'a') == L'a');
assert(t.translate(L'B') == L'B');
assert(t.translate(L'c') == L'c');
}
}

View File

@@ -0,0 +1,62 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// charT translate_nocase(charT c) const;
#include <regex>
#include <cassert>
int main()
{
{
std::regex_traits<char> t;
assert(t.translate_nocase(' ') == ' ');
assert(t.translate_nocase('A') == 'a');
assert(t.translate_nocase('\x07') == '\x07');
assert(t.translate_nocase('.') == '.');
assert(t.translate_nocase('a') == 'a');
assert(t.translate_nocase('1') == '1');
assert(t.translate_nocase('\xDA') == '\xDA');
assert(t.translate_nocase('\xFA') == '\xFA');
t.imbue(std::locale("en_US"));
assert(t.translate_nocase(' ') == ' ');
assert(t.translate_nocase('A') == 'a');
assert(t.translate_nocase('\x07') == '\x07');
assert(t.translate_nocase('.') == '.');
assert(t.translate_nocase('a') == 'a');
assert(t.translate_nocase('1') == '1');
assert(t.translate_nocase('\xDA') == '\xDA');
assert(t.translate_nocase('\xFA') == '\xFA');
}
{
std::regex_traits<wchar_t> t;
assert(t.translate_nocase(L' ') == L' ');
assert(t.translate_nocase(L'A') == L'a');
assert(t.translate_nocase(L'\x07') == L'\x07');
assert(t.translate_nocase(L'.') == L'.');
assert(t.translate_nocase(L'a') == L'a');
assert(t.translate_nocase(L'1') == L'1');
assert(t.translate_nocase(L'\xDA') == L'\xDA');
assert(t.translate_nocase(L'\xFA') == L'\xFA');
t.imbue(std::locale("en_US"));
assert(t.translate_nocase(L' ') == L' ');
assert(t.translate_nocase(L'A') == L'a');
assert(t.translate_nocase(L'\x07') == L'\x07');
assert(t.translate_nocase(L'.') == L'.');
assert(t.translate_nocase(L'a') == L'a');
assert(t.translate_nocase(L'1') == L'1');
assert(t.translate_nocase(L'\xDA') == L'\xFA');
assert(t.translate_nocase(L'\xFA') == L'\xFA');
}
}

View File

@@ -0,0 +1,32 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT>
// struct regex_traits
// {
// public:
// typedef charT char_type;
// typedef basic_string<char_type> string_type;
// typedef locale locale_type;
#include <regex>
#include <type_traits>
int main()
{
static_assert((std::is_same<std::regex_traits<char>::char_type, char>::value), "");
static_assert((std::is_same<std::regex_traits<char>::string_type, std::string>::value), "");
static_assert((std::is_same<std::regex_traits<char>::locale_type, std::locale>::value), "");
static_assert((std::is_same<std::regex_traits<wchar_t>::char_type, wchar_t>::value), "");
static_assert((std::is_same<std::regex_traits<wchar_t>::string_type, std::wstring>::value), "");
static_assert((std::is_same<std::regex_traits<wchar_t>::locale_type, std::locale>::value), "");
}

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class charT> struct regex_traits;
// int value(charT ch, int radix) const;
#include <regex>
int main()
{
#error value not implemented
}