Cogs.Core
StdString.h
1#pragma once
2
3#include "StringView.h"
4
5#include <ostream>
6
7namespace Cogs
8{
17 inline std::ostream & operator<<(std::ostream & stream, const Cogs::StringView & stringView)
18 {
19 stream << std::string_view(stringView);
20
21 return stream;
22 }
23
24 inline std::string& stringReplaceAll(std::string& str, std::string_view from, std::string_view to)
25 {
26 if (!from.empty()) {
27 for (size_t startPos = str.find(from, 0); startPos != std::string::npos; startPos = str.find(from, startPos + to.length())) {
28 str.replace(startPos, from.length(), to);
29 }
30 }
31 return str;
32 }
33
34 inline bool stringStartsWith(std::string_view str, std::string_view beginning) {
35 return str.find(beginning) == 0;
36 }
37
38 inline bool stringEndsWith(std::string_view str, std::string_view ending) {
39 size_t strLen = str.size();
40 size_t endLen = ending.size();
41
42 if (endLen <= strLen) {
43 return str.rfind(ending) == (strLen - endLen);
44 }
45 return false;
46 }
47
48 inline std::string stringToLower(std::string_view input)
49 {
50 std::string output;
51
52 output.reserve(input.size());
53 for (const char c : input) {
54 output += static_cast<char>(std::tolower(c));
55 }
56 return output;
57 }
58
59 inline std::string stringToUpper(std::string_view input)
60 {
61 std::string output;
62
63 output.reserve(input.size());
64 for (const char c : input) {
65 output += static_cast<char>(std::toupper(c));
66 }
67 return output;
68 }
69}
70
71namespace std
72{
76 template<>
77 struct hash<Cogs::StringView>
78 {
82 size_t operator()(const Cogs::StringView & v) const noexcept
83 {
84 return Cogs::hash(v);
85 }
86 };
87}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
std::ostream & operator<<(std::ostream &stream, const Cogs::StringView &stringView)
Stream operator out provided to make it possible to insert Cogs::StringView into streams.
Definition: StdString.h:17
STL namespace.
size_t operator()(const Cogs::StringView &v) const noexcept
Hash operator.
Definition: StdString.h:82