1// Copyright 2023 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14#pragma once 15 16#include "pw_polyfill/standard_library/namespace.h" 17 18_PW_POLYFILL_BEGIN_NAMESPACE_STD 19 20// NOT IMPLEMENTED: Some functions and overloads are missing. 21template <typename T> 22class char_traits { 23 public: 24 static constexpr void assign(T& dest, const T& source) noexcept { 25 dest = source; 26 } 27 28 static constexpr T* assign(T* dest, decltype(sizeof(0)) count, T value) { 29 for (decltype(sizeof(0)) i = 0; i < count; ++i) { 30 dest[i] = value; 31 } 32 return dest; 33 } 34 35 static constexpr bool eq(T lhs, T rhs) noexcept { return lhs == rhs; } 36 37 static constexpr bool lt(T lhs, T rhs) noexcept { return lhs < rhs; } 38 39 static constexpr T* move(T* dest, 40 const T* source, 41 decltype(sizeof(0)) count) { 42 if (dest < source) { 43 copy(dest, source, count); 44 } else if (source < dest) { 45 for (decltype(sizeof(0)) i = count; i != 0; --i) { 46 assign(dest[i - 1], source[i - 1]); 47 } 48 } 49 return dest; 50 } 51 52 static constexpr T* copy(T* dest, 53 const T* source, 54 decltype(sizeof(0)) count) { 55 for (decltype(sizeof(0)) i = 0; i < count; ++i) { 56 char_traits<T>::assign(dest[i], source[i]); 57 } 58 return dest; 59 } 60 61 static constexpr int compare(const T* lhs, 62 const T* rhs, 63 decltype(sizeof(0)) count) { 64 for (decltype(sizeof(0)) i = 0; i < count; ++i) { 65 if (!eq(lhs[i], rhs[i])) { 66 return lt(lhs[i], rhs[i]) ? -1 : 1; 67 } 68 } 69 return 0; 70 } 71}; 72 73_PW_POLYFILL_END_NAMESPACE_STD 74