Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_STRING_H_
34#define DVD_STRING_H_
35
36#include "STLString.h"
37
38#include <fmt/format.h>
39
40namespace Divide
41{
42 template<size_t N>
43 class Str : public eastl::fixed_string<char, N, true>
44 {
45 public:
46 using Base = eastl::fixed_string<char, N, true>;
47 using Base::Base;
48
49 Str(const char* str, const size_t length, [[maybe_unused]] dvd_allocator<char>& allocator) : Base(str, length) {}
50
51 Str(const string& str) : Base(str.c_str()) {}
52 Str(const std::string_view str) : Base(str.data(), str.size()) {}
53
54 operator std::string_view() const noexcept
55 {
56 return std::string_view( Base::c_str(), Base::size() );
57 }
58 };
59
60 template<size_t N>
61 Str<N> operator+( const char* other, const Str<N>& base )
62 {
63 Str<N> ret( other );
64 ret.append( base );
65 return ret;
66
67 }
68
69 template<size_t N>
70 Str<N> operator+( const std::string_view other, const Str<N>& base )
71 {
72 Str<N> ret( other );
73 ret.append( base );
74 return ret;
75 }
76
77 template<size_t N>
78 Str<N> operator+( const Str<N>& base, const char* other )
79 {
80 Str<N> ret = base;
81 ret.append( other );
82 return ret;
83 }
84
85 template<size_t N>
86 Str<N> operator+( const Str<N>& base, const std::string_view other )
87 {
88 Str<N> ret = base;
89 ret.append( other.data(), other.size() );
90 return ret;
91 }
92
93 template<typename T>
95}//namespace Divide
96
97template<size_t N>
98struct fmt::formatter<Divide::Str<N>>
99{
100 constexpr auto parse( format_parse_context& ctx ) { return ctx.begin(); }
101
102 template <typename FormatContext>
103 auto format( const Divide::Str<N>& str, FormatContext& ctx ) -> decltype(ctx.out())
104 {
105 return fmt::format_to( ctx.out(), "{}", str.c_str() );
106 }
107};
108
109namespace std
110{
111 template<size_t N>
112 struct hash<Divide::Str<N> >
113 {
114 size_t operator()( const Divide::Str<N>& str ) const
115 {
116 const std::string_view view = str;
117 return std::hash<std::string_view>{}(view);
118 }
119 };
120}
121
122#endif //DVD_STRING_H_
mi_stl_allocator< T > dvd_allocator
Str(const string &str)
Definition: String.h:51
eastl::fixed_string< char, N, true > Base
Definition: String.h:46
Str(const std::string_view str)
Definition: String.h:52
Str(const char *str, const size_t length, dvd_allocator< char > &allocator)
Definition: String.h:49
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
Str< N > operator+(const char *other, const Str< N > &base)
Definition: String.h:61
auto format(const Divide::Str< N > &str, FormatContext &ctx) -> decltype(ctx.out())
Definition: String.h:103
constexpr auto parse(format_parse_context &ctx)
Definition: String.h:100
size_t operator()(const Divide::Str< N > &str) const
Definition: String.h:114