|
7 | 7 |
|
8 | 8 | use std::convert::Infallible;
|
9 | 9 | use std::ffi::c_char;
|
| 10 | +use std::fmt; |
10 | 11 | use std::fmt::Write;
|
11 |
| -use std::str::FromStr; |
12 |
| -use std::{cmp, fmt, ops}; |
13 | 12 |
|
14 | 13 | use godot_ffi as sys;
|
15 | 14 | use sys::types::OpaqueString;
|
16 | 15 | use sys::{ffi_methods, interface_fn, GodotFfi};
|
17 | 16 |
|
18 | 17 | use crate::builtin::{inner, NodePath, StringName, Variant};
|
19 |
| -use crate::meta; |
20 | 18 | use crate::meta::AsArg;
|
| 19 | +use crate::{impl_shared_string_api, meta}; |
21 | 20 |
|
22 | 21 | /// Godot's reference counted string type.
|
23 | 22 | ///
|
@@ -112,134 +111,6 @@ impl GString {
|
112 | 111 | }
|
113 | 112 | }
|
114 | 113 |
|
115 |
| - /// Returns a substring of this, as another `GString`. |
116 |
| - pub fn substr(&self, range: impl ops::RangeBounds<usize>) -> Self { |
117 |
| - let (from, len) = super::to_fromlen_pair(range); |
118 |
| - |
119 |
| - self.as_inner().substr(from, len) |
120 |
| - } |
121 |
| - |
122 |
| - /// Format a string using substitutions from an array or dictionary. |
123 |
| - /// |
124 |
| - /// See Godot's [`String.format()`](https://docs.godotengine.org/en/stable/classes/class_string.html#class-string-method-format). |
125 |
| - pub fn format(&self, array_or_dict: &Variant) -> Self { |
126 |
| - self.as_inner().format(array_or_dict, "{_}") |
127 |
| - } |
128 |
| - |
129 |
| - /// Format a string using substitutions from an array or dictionary + custom placeholder. |
130 |
| - /// |
131 |
| - /// See Godot's [`String.format()`](https://docs.godotengine.org/en/stable/classes/class_string.html#class-string-method-format). |
132 |
| - pub fn format_with_placeholder( |
133 |
| - &self, |
134 |
| - array_or_dict: &Variant, |
135 |
| - placeholder: impl AsArg<GString>, |
136 |
| - ) -> Self { |
137 |
| - self.as_inner().format(array_or_dict, placeholder) |
138 |
| - } |
139 |
| - |
140 |
| - /// Case-sensitive, lexicographic comparison to another string. |
141 |
| - /// |
142 |
| - /// Returns the `Ordering` relation of `self` towards `to`. Ordering is determined by the Unicode code points of each string, which roughly |
143 |
| - /// matches the alphabetical order. |
144 |
| - /// |
145 |
| - /// See also [`nocasecmp_to()`](Self::nocasecmp_to), [`naturalcasecmp_to()`](Self::naturalcasecmp_to), [`filecasecmp_to()`](Self::filecasecmp_to). |
146 |
| - pub fn casecmp_to(&self, to: impl AsArg<GString>) -> cmp::Ordering { |
147 |
| - sys::i64_to_ordering(self.as_inner().casecmp_to(to)) |
148 |
| - } |
149 |
| - |
150 |
| - /// Case-**insensitive**, lexicographic comparison to another string. |
151 |
| - /// |
152 |
| - /// Returns the `Ordering` relation of `self` towards `to`. Ordering is determined by the Unicode code points of each string, which roughly |
153 |
| - /// matches the alphabetical order. |
154 |
| - /// |
155 |
| - /// See also [`casecmp_to()`](Self::casecmp_to), [`naturalcasecmp_to()`](Self::naturalcasecmp_to), [`filecasecmp_to()`](Self::filecasecmp_to). |
156 |
| - pub fn nocasecmp_to(&self, to: impl AsArg<GString>) -> cmp::Ordering { |
157 |
| - sys::i64_to_ordering(self.as_inner().nocasecmp_to(to)) |
158 |
| - } |
159 |
| - |
160 |
| - /// Case-sensitive, **natural-order** comparison to another string. |
161 |
| - /// |
162 |
| - /// Returns the `Ordering` relation of `self` towards `to`. Ordering is determined by the Unicode code points of each string, which roughly |
163 |
| - /// matches the alphabetical order. |
164 |
| - /// |
165 |
| - /// When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, |
166 |
| - /// instead of the single digit's value. A sorted sequence of numbered strings will be `["1", "2", "3", ...]`, not `["1", "10", "2", "3", ...]`. |
167 |
| - /// |
168 |
| - /// With different string lengths, returns `Ordering::Greater` if this string is longer than the `to` string, or `Ordering::Less` if shorter. |
169 |
| - /// |
170 |
| - /// See also [`casecmp_to()`](Self::casecmp_to), [`naturalnocasecmp_to()`](Self::naturalnocasecmp_to), [`filecasecmp_to()`](Self::filecasecmp_to). |
171 |
| - pub fn naturalcasecmp_to(&self, to: impl AsArg<GString>) -> cmp::Ordering { |
172 |
| - sys::i64_to_ordering(self.as_inner().naturalcasecmp_to(to)) |
173 |
| - } |
174 |
| - |
175 |
| - /// Case-insensitive, **natural-order** comparison to another string. |
176 |
| - /// |
177 |
| - /// Returns the `Ordering` relation of `self` towards `to`. Ordering is determined by the Unicode code points of each string, which roughly |
178 |
| - /// matches the alphabetical order. |
179 |
| - /// |
180 |
| - /// When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, |
181 |
| - /// instead of the single digit's value. A sorted sequence of numbered strings will be `["1", "2", "3", ...]`, not `["1", "10", "2", "3", ...]`. |
182 |
| - /// |
183 |
| - /// With different string lengths, returns `Ordering::Greater` if this string is longer than the `to` string, or `Ordering::Less` if shorter. |
184 |
| - /// |
185 |
| - /// See also [`casecmp_to()`](Self::casecmp_to), [`naturalcasecmp_to()`](Self::naturalcasecmp_to), [`filecasecmp_to()`](Self::filecasecmp_to). |
186 |
| - pub fn naturalnocasecmp_to(&self, to: impl AsArg<GString>) -> cmp::Ordering { |
187 |
| - sys::i64_to_ordering(self.as_inner().naturalnocasecmp_to(to)) |
188 |
| - } |
189 |
| - |
190 |
| - /// Case-sensitive, filename-oriented comparison to another string. |
191 |
| - /// |
192 |
| - /// Like [`naturalcasecmp_to()`][Self::naturalcasecmp_to], but prioritizes strings that begin with periods (`.`) and underscores (`_`) before |
193 |
| - /// any other character. Useful when sorting folders or file names. |
194 |
| - /// |
195 |
| - /// See also [`casecmp_to()`](Self::casecmp_to), [`naturalcasecmp_to()`](Self::naturalcasecmp_to), [`filenocasecmp_to()`](Self::filenocasecmp_to). |
196 |
| - #[cfg(since_api = "4.3")] |
197 |
| - pub fn filecasecmp_to(&self, to: impl AsArg<GString>) -> cmp::Ordering { |
198 |
| - sys::i64_to_ordering(self.as_inner().filecasecmp_to(to)) |
199 |
| - } |
200 |
| - |
201 |
| - /// Case-insensitive, filename-oriented comparison to another string. |
202 |
| - /// |
203 |
| - /// Like [`naturalnocasecmp_to()`][Self::naturalnocasecmp_to], but prioritizes strings that begin with periods (`.`) and underscores (`_`) before |
204 |
| - /// any other character. Useful when sorting folders or file names. |
205 |
| - /// |
206 |
| - /// See also [`casecmp_to()`](Self::casecmp_to), [`naturalcasecmp_to()`](Self::naturalcasecmp_to), [`filecasecmp_to()`](Self::filecasecmp_to). |
207 |
| - #[cfg(since_api = "4.3")] |
208 |
| - pub fn filenocasecmp_to(&self, to: impl AsArg<GString>) -> cmp::Ordering { |
209 |
| - sys::i64_to_ordering(self.as_inner().filenocasecmp_to(to)) |
210 |
| - } |
211 |
| - |
212 |
| - /// Splits the string using a string delimiter and returns the substring at index `slice`. |
213 |
| - /// |
214 |
| - /// Returns the original string if delimiter does not occur in the string. Returns `None` if `slice` is out of bounds. |
215 |
| - /// |
216 |
| - /// This is faster than [`split()`][Self::split], if you only need one substring. |
217 |
| - pub fn get_slice(&self, delimiter: impl AsArg<GString>, slice: usize) -> Option<GString> { |
218 |
| - let sliced = self.as_inner().get_slice(delimiter, slice as i64); |
219 |
| - |
220 |
| - // Note: self="" always returns None. |
221 |
| - super::populated_or_none(sliced) |
222 |
| - } |
223 |
| - |
224 |
| - /// Returns the total number of slices, when the string is split with the given delimiter. |
225 |
| - /// |
226 |
| - /// See also [`split()`][Self::split] and [`get_slice()`][Self::get_slice]. |
227 |
| - pub fn get_slice_count(&self, delimiter: impl AsArg<GString>) -> usize { |
228 |
| - self.as_inner().get_slice_count(delimiter) as usize |
229 |
| - } |
230 |
| - |
231 |
| - /// Splits the string using a Unicode char `delimiter` and returns the substring at index `slice`. |
232 |
| - /// |
233 |
| - /// Returns the original string if delimiter does not occur in the string. Returns `None` if `slice` is out of bounds. |
234 |
| - /// |
235 |
| - /// This is faster than [`split()`][Self::split], if you only need one substring. |
236 |
| - pub fn get_slicec(&self, delimiter: char, slice: usize) -> Option<GString> { |
237 |
| - let sliced = self.as_inner().get_slicec(delimiter as i64, slice as i64); |
238 |
| - |
239 |
| - // Note: self="" always returns None. |
240 |
| - super::populated_or_none(sliced) |
241 |
| - } |
242 |
| - |
243 | 114 | ffi_methods! {
|
244 | 115 | type sys::GDExtensionStringPtr = *mut Self;
|
245 | 116 |
|
@@ -289,7 +160,7 @@ impl GString {
|
289 | 160 | }
|
290 | 161 |
|
291 | 162 | meta::declare_arg_method! {
|
292 |
| - /// Use as argument for an [`impl AsArg<StringName|NodePath>`][crate::AsArg] parameter. |
| 163 | + /// Use as argument for an [`impl AsArg<StringName|NodePath>`][crate::meta::AsArg] parameter. |
293 | 164 | ///
|
294 | 165 | /// This is a convenient way to convert arguments of similar string types.
|
295 | 166 | ///
|
@@ -342,6 +213,12 @@ impl_builtin_traits! {
|
342 | 213 | }
|
343 | 214 | }
|
344 | 215 |
|
| 216 | +impl_shared_string_api! { |
| 217 | + builtin: GString, |
| 218 | + find_builder: ExGStringFind, |
| 219 | + split_builder: ExGStringSplit, |
| 220 | +} |
| 221 | + |
345 | 222 | impl fmt::Display for GString {
|
346 | 223 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
347 | 224 | for ch in self.chars() {
|
@@ -438,7 +315,7 @@ impl From<GString> for String {
|
438 | 315 | }
|
439 | 316 | }
|
440 | 317 |
|
441 |
| -impl FromStr for GString { |
| 318 | +impl std::str::FromStr for GString { |
442 | 319 | type Err = Infallible;
|
443 | 320 |
|
444 | 321 | fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
0 commit comments