Skip to content

Commit c011aef

Browse files
committed
GString::get_slice* functions (WIP)
1 parent 07a553c commit c011aef

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,6 @@ pub fn is_builtin_method_exposed(builtin_ty: &TyName, godot_method_name: &str) -
235235
// TODO maybe consider renaming "match_" -> "matches". The "*n" could technically be "*_n", but is probably OK.
236236

237237
// GString
238-
| ("String", "get_slice")
239-
| ("String", "get_slicec")
240-
| ("String", "get_slice_count")
241238
| ("String", "find")
242239
| ("String", "findn")
243240
| ("String", "count")

godot-core/src/builtin/string/gstring.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,37 @@ impl GString {
209209
sys::i64_to_ordering(self.as_inner().filenocasecmp_to(to))
210210
}
211211

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+
212243
ffi_methods! {
213244
type sys::GDExtensionStringPtr = *mut Self;
214245

godot-core/src/builtin/string/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,11 @@ where
7474

7575
(from, len)
7676
}
77+
78+
fn populated_or_none(s: GString) -> Option<GString> {
79+
if s.is_empty() {
80+
None
81+
} else {
82+
Some(s)
83+
}
84+
}

0 commit comments

Comments
 (0)