-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
159 lines (134 loc) · 4.21 KB
/
string.cpp
File metadata and controls
159 lines (134 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright (C) 2015-present The DotCpp Authors.
This file is part of .C++, a native C++ implementation of
popular .NET class library APIs developed to facilitate
code reuse between C# and C++.
http://github.com/dotcpp/dotcpp (source)
http://dotcpp.org (documentation)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <dot/precompiled.hpp>
#define FMT_HEADER_ONLY
#include <fmt/format.h>
#include <dot/implement.hpp>
#include <dot/system/string.hpp>
#include <dot/system/object.hpp>
#include <dot/system/nullable.hpp>
#include <dot/system/type.hpp>
namespace dot
{
/// Empty string.
string string::empty = make_string("");
dot::type string_impl::typeof()
{
static dot::type result = []()->dot::type
{
dot::type t = dot::make_type_builder<string_impl>("System", "string")
->build();
return t;
}();
return result;
}
dot::type string_impl::get_type()
{
return typeof();
}
bool string_impl::equals(object obj)
{
if (this == &(*obj)) return true;
if (obj.is<string>())
{
return *this == *obj.as<string>();
}
return false;
}
size_t string_impl::hash_code()
{
return std::hash<std::string>()(*this);
}
string string_impl::to_string()
{
return this;
}
/// Determines whether the end of this
/// string matches the specified string.
bool string_impl::ends_with(const string& value)
{
int p = length() - value->length();
if (p >= 0 && substr(p, value->length()) == *value)
return true;
return false;
}
/// Determines whether the beginning of this
/// string matches the specified string.
bool string_impl::starts_with(const string& value)
{
int p = length() - value->length();
if (p >= 0 && substr(0, value->length()) == *value)
return true;
return false;
}
/// Retrieves a substring which starts at the specified
/// character position and has the specified length.
string string_impl::substring(int startIndex, int length)
{
return make_string(this->substr(startIndex, length));
}
int string_impl::index_of_any(list<char> anyOf)
{
size_t pos = find_first_of(anyOf->data(), 0, anyOf->size());
if (pos != std::string::npos)
return pos;
return -1;
}
string string_impl::remove(int startIndex)
{
return make_string(*this)->erase(startIndex);
}
string string_impl::remove(int startIndex, int count)
{
return make_string(*this)->erase(startIndex, count);
}
string string_impl::replace(const char oldChar, const char newChar) const
{
string make_str = *this;
std::replace(make_str->begin(), make_str->end(), oldChar, newChar);
return make_str;
}
bool string::is_null_or_empty(string value)
{
if (value == nullptr || value->empty())
return true;
return false;
}
/// Case sensitive comparison to object.
bool string::operator==(const object& rhs) const
{
// If rhs is null, return false. Otherwise, check if
// the other object is a string. If yes, compare by value.
// If no, return false.
if (rhs == nullptr)
{
return false;
}
else
{
string rhsStr = rhs.as<string>();
if (rhsStr != nullptr) return operator==(rhsStr);
else return false;
}
}
/// Non-template implementation of string.Format.
std::string string::format_impl(fmt::string_view format_str, fmt::format_args args)
{
return fmt::vformat(format_str, args);
}
}