char_traits.h revision 743c6a2694bf16bf29d516c906363199a7bccf86
1/* -*- c++ -*- */
2/*
3 * Copyright (C) 2010 The Android Open Source Project
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *  * Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 *  * Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef ANDROID_ASTL_CHAR_TRAITS_H__
31#define ANDROID_ASTL_CHAR_TRAITS_H__
32
33#include <ios_pos_types.h>  // For streampos
34#include <cstdio>           // For EOF
35#include <cstring>          // For memcmp, memchr, strlen
36
37namespace std {
38
39/**
40 * char_traits defines the basic types and constants (eof) used in
41 * string and stream as well as basic char manipulations.
42 * Android's support only char. The state_type is missing because we
43 * don't support multibyte strings.
44 */
45
46template<class _CharT> struct char_traits {
47    // Empty on purpose. You should use char_traits<char> only.
48};
49
50template<>
51struct char_traits<char>
52{
53    typedef char       char_type;
54    typedef int        int_type;
55    typedef streampos  pos_type;
56    typedef streamoff  off_type;
57
58    static void assign(char& lhs, const char& rhs) { lhs = rhs; }
59
60    static bool eq(const char& lhs, const char& rhs) { return lhs == rhs; }
61
62    static bool lt(const char& lhs, const char& rhs) { return lhs < rhs; }
63
64    static int compare(const char* lhs, const char* rhs, size_t n)
65    { return std::memcmp(lhs, rhs, n); }
66
67    static size_t length(const char* str) { return std::strlen(str); }
68
69    static const char* find(const char* str, size_t n, const char& c)
70    { return static_cast<const char*>(std::memchr(str, c, n)); }
71
72    static char* move(char* lhs, const char* rhs, size_t n)
73    { return static_cast<char*>(std::memmove(lhs, rhs, n)); }
74
75    static char* copy(char* lhs, const char* rhs, size_t n)
76    { return static_cast<char*>(std::memcpy(lhs, rhs, n)); }
77
78    // Fill 'lhs' with 'n' occurences of 'c'
79    static char* assign(char* lhs, size_t n, char c)
80    { return static_cast<char*>(std::memset(lhs, c, n)); }
81
82    static char to_char(const int_type& c) { return static_cast<char>(c); }
83
84    static int_type to_int_type(const char& c)
85    { return static_cast<int_type>(static_cast<unsigned char>(c)); }
86
87    static bool eq_int_type(const int_type& lhs, const int_type& rhs)
88    { return lhs == rhs; }
89
90    static int_type eof() { return static_cast<int_type>(EOF); }
91
92    static int_type not_eof(const int_type& c)
93    { return (c == eof()) ? 0 : c; }
94  };
95
96}  // namespace std
97
98#endif
99