2014-06-07 06:33:40 +08:00
|
|
|
// -*- C++ -*-
|
2017-01-19 04:10:25 +08:00
|
|
|
//===-------------------- constexpr_char_traits ---------------------------===//
|
2014-06-07 06:33:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-06-07 06:33:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _CONSTEXPR_CHAR_TRAITS
|
|
|
|
#define _CONSTEXPR_CHAR_TRAITS
|
|
|
|
|
|
|
|
#include <string>
|
2017-02-17 09:17:10 +08:00
|
|
|
#include <cassert>
|
2014-06-07 06:33:40 +08:00
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
#include "test_macros.h"
|
2014-06-07 06:33:40 +08:00
|
|
|
|
|
|
|
template <class _CharT>
|
|
|
|
struct constexpr_char_traits
|
|
|
|
{
|
|
|
|
typedef _CharT char_type;
|
|
|
|
typedef int int_type;
|
|
|
|
typedef std::streamoff off_type;
|
|
|
|
typedef std::streampos pos_type;
|
|
|
|
typedef std::mbstate_t state_type;
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{__c1 = __c2;}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return __c1 == __c2;}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return __c1 < __c2;}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR_CXX14 int compare(const char_type* __s1, const char_type* __s2, size_t __n);
|
|
|
|
static TEST_CONSTEXPR_CXX14 size_t length(const char_type* __s);
|
|
|
|
static TEST_CONSTEXPR_CXX14 const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
|
|
|
|
static TEST_CONSTEXPR_CXX14 char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
|
|
|
|
static TEST_CONSTEXPR_CXX14 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
|
|
|
|
static TEST_CONSTEXPR_CXX14 char_type* assign(char_type* __s, size_t __n, char_type __a);
|
2014-06-07 06:33:40 +08:00
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR int_type not_eof(int_type __c) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return eq_int_type(__c, eof()) ? ~eof() : __c;}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return char_type(__c);}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR int_type to_int_type(char_type __c) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return int_type(__c);}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return __c1 == __c2;}
|
|
|
|
|
2016-04-22 18:33:56 +08:00
|
|
|
static TEST_CONSTEXPR int_type eof() TEST_NOEXCEPT
|
2014-06-07 06:33:40 +08:00
|
|
|
{return int_type(EOF);}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class _CharT>
|
2016-04-22 18:33:56 +08:00
|
|
|
TEST_CONSTEXPR_CXX14 int
|
2014-06-07 06:33:40 +08:00
|
|
|
constexpr_char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
|
|
|
|
{
|
|
|
|
for (; __n; --__n, ++__s1, ++__s2)
|
|
|
|
{
|
|
|
|
if (lt(*__s1, *__s2))
|
|
|
|
return -1;
|
|
|
|
if (lt(*__s2, *__s1))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _CharT>
|
2016-04-22 18:33:56 +08:00
|
|
|
TEST_CONSTEXPR_CXX14 size_t
|
2014-06-07 06:33:40 +08:00
|
|
|
constexpr_char_traits<_CharT>::length(const char_type* __s)
|
|
|
|
{
|
|
|
|
size_t __len = 0;
|
|
|
|
for (; !eq(*__s, char_type(0)); ++__s)
|
|
|
|
++__len;
|
|
|
|
return __len;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _CharT>
|
2016-04-22 18:33:56 +08:00
|
|
|
TEST_CONSTEXPR_CXX14 const _CharT*
|
2014-06-07 06:33:40 +08:00
|
|
|
constexpr_char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
|
|
|
|
{
|
|
|
|
for (; __n; --__n)
|
|
|
|
{
|
|
|
|
if (eq(*__s, __a))
|
|
|
|
return __s;
|
|
|
|
++__s;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _CharT>
|
2016-04-22 18:33:56 +08:00
|
|
|
TEST_CONSTEXPR_CXX14 _CharT*
|
2014-06-07 06:33:40 +08:00
|
|
|
constexpr_char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
|
|
|
|
{
|
|
|
|
char_type* __r = __s1;
|
|
|
|
if (__s1 < __s2)
|
|
|
|
{
|
|
|
|
for (; __n; --__n, ++__s1, ++__s2)
|
|
|
|
assign(*__s1, *__s2);
|
|
|
|
}
|
|
|
|
else if (__s2 < __s1)
|
|
|
|
{
|
|
|
|
__s1 += __n;
|
|
|
|
__s2 += __n;
|
|
|
|
for (; __n; --__n)
|
|
|
|
assign(*--__s1, *--__s2);
|
|
|
|
}
|
|
|
|
return __r;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _CharT>
|
2016-04-22 18:33:56 +08:00
|
|
|
TEST_CONSTEXPR_CXX14 _CharT*
|
2014-06-07 06:33:40 +08:00
|
|
|
constexpr_char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
|
|
|
|
{
|
2017-02-17 09:17:10 +08:00
|
|
|
assert(__s2 < __s1 || __s2 >= __s1+__n);
|
2014-06-07 06:33:40 +08:00
|
|
|
char_type* __r = __s1;
|
|
|
|
for (; __n; --__n, ++__s1, ++__s2)
|
|
|
|
assign(*__s1, *__s2);
|
|
|
|
return __r;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _CharT>
|
2016-04-22 18:33:56 +08:00
|
|
|
TEST_CONSTEXPR_CXX14 _CharT*
|
2014-06-07 06:33:40 +08:00
|
|
|
constexpr_char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
|
|
|
|
{
|
|
|
|
char_type* __r = __s;
|
|
|
|
for (; __n; --__n, ++__s)
|
|
|
|
assign(*__s, __a);
|
|
|
|
return __r;
|
|
|
|
}
|
|
|
|
|
2015-02-11 01:32:49 +08:00
|
|
|
#endif // _CONSTEXPR_CHAR_TRAITS
|