Skip to content

Commit 8eaf184

Browse files
authored
Merge pull request #980 from godot-rust/feature/string-generated-methods
Add many `GString`/`StringName` methods
2 parents 334a8d3 + 2055bdf commit 8eaf184

File tree

11 files changed

+947
-41
lines changed

11 files changed

+947
-41
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Cutting-edge API docs of the `master` branch are available [here](https://godot-
1414
- [v0.1.1](#v011), [v0.1.2](#v012), [v0.1.3](#v013)
1515

1616

17+
1718
## [v0.2.1](https://docs.rs/godot/0.2.1)
1819

1920
_8 December 2024_

godot-codegen/src/special_cases/special_cases.rs

+153-8
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,156 @@ pub fn is_method_private(class_or_builtin_ty: &TyName, godot_method_name: &str)
243243
pub fn is_builtin_method_exposed(builtin_ty: &TyName, godot_method_name: &str) -> bool {
244244
match (builtin_ty.godot_ty.as_str(), godot_method_name) {
245245
// GString
246-
| ("String", "casecmp_to")
247-
| ("String", "nocasecmp_to")
246+
| ("String", "begins_with")
247+
| ("String", "ends_with")
248+
| ("String", "is_subsequence_of")
249+
| ("String", "is_subsequence_ofn")
250+
| ("String", "bigrams")
251+
| ("String", "similarity")
252+
| ("String", "replace")
253+
| ("String", "replacen")
254+
| ("String", "repeat")
255+
| ("String", "reverse")
256+
| ("String", "capitalize")
257+
| ("String", "to_camel_case")
258+
| ("String", "to_pascal_case")
259+
| ("String", "to_snake_case")
260+
| ("String", "split_floats")
261+
| ("String", "join")
262+
| ("String", "to_upper")
263+
| ("String", "to_lower")
264+
| ("String", "left")
265+
| ("String", "right")
266+
| ("String", "strip_edges")
267+
| ("String", "strip_escapes")
268+
| ("String", "lstrip")
269+
| ("String", "rstrip")
270+
| ("String", "get_extension")
271+
| ("String", "get_basename")
272+
| ("String", "path_join")
273+
| ("String", "indent")
274+
| ("String", "dedent")
275+
| ("String", "md5_text")
276+
| ("String", "sha1_text")
277+
| ("String", "sha256_text")
278+
| ("String", "md5_buffer")
279+
| ("String", "sha1_buffer")
280+
| ("String", "sha256_buffer")
281+
| ("String", "is_empty")
282+
| ("String", "contains")
283+
| ("String", "containsn")
284+
| ("String", "is_absolute_path")
285+
| ("String", "is_relative_path")
286+
| ("String", "simplify_path")
287+
| ("String", "get_base_dir")
288+
| ("String", "get_file")
289+
| ("String", "xml_escape")
290+
| ("String", "xml_unescape")
291+
| ("String", "uri_encode")
292+
| ("String", "uri_decode")
293+
| ("String", "c_escape")
294+
| ("String", "c_unescape")
295+
| ("String", "json_escape")
296+
| ("String", "validate_node_name")
297+
| ("String", "validate_filename")
298+
| ("String", "is_valid_identifier")
299+
| ("String", "is_valid_int")
300+
| ("String", "is_valid_float")
301+
| ("String", "is_valid_hex_number")
302+
| ("String", "is_valid_html_color")
303+
| ("String", "is_valid_ip_address")
304+
| ("String", "is_valid_filename")
305+
| ("String", "to_int")
306+
| ("String", "to_float")
307+
| ("String", "hex_to_int")
308+
| ("String", "bin_to_int")
309+
| ("String", "trim_prefix")
310+
| ("String", "trim_suffix")
311+
| ("String", "to_ascii_buffer")
312+
| ("String", "to_utf8_buffer")
313+
| ("String", "to_utf16_buffer")
314+
| ("String", "to_utf32_buffer")
315+
| ("String", "hex_decode")
316+
| ("String", "to_wchar_buffer")
317+
| ("String", "num_scientific")
318+
| ("String", "num")
319+
| ("String", "num_int64")
320+
| ("String", "num_uint64")
321+
| ("String", "chr")
322+
| ("String", "humanize_size")
248323

249324
// StringName
250-
| ("StringName", "casecmp_to")
325+
| ("StringName", "begins_with")
326+
| ("StringName", "ends_with")
327+
| ("StringName", "is_subsequence_of")
328+
| ("StringName", "is_subsequence_ofn")
329+
| ("StringName", "bigrams")
330+
| ("StringName", "similarity")
331+
| ("StringName", "replace")
332+
| ("StringName", "replacen")
333+
| ("StringName", "repeat")
334+
| ("StringName", "reverse")
335+
| ("StringName", "capitalize")
336+
| ("StringName", "to_camel_case")
337+
| ("StringName", "to_pascal_case")
338+
| ("StringName", "to_snake_case")
339+
| ("StringName", "split_floats")
340+
| ("StringName", "join")
341+
| ("StringName", "to_upper")
342+
| ("StringName", "to_lower")
343+
| ("StringName", "left")
344+
| ("StringName", "right")
345+
| ("StringName", "strip_edges")
346+
| ("StringName", "strip_escapes")
347+
| ("StringName", "lstrip")
348+
| ("StringName", "rstrip")
349+
| ("StringName", "get_extension")
350+
| ("StringName", "get_basename")
351+
| ("StringName", "path_join")
352+
| ("StringName", "indent")
353+
| ("StringName", "dedent")
354+
| ("StringName", "md5_text")
355+
| ("StringName", "sha1_text")
356+
| ("StringName", "sha256_text")
357+
| ("StringName", "md5_buffer")
358+
| ("StringName", "sha1_buffer")
359+
| ("StringName", "sha256_buffer")
360+
| ("StringName", "is_empty")
361+
| ("StringName", "contains")
362+
| ("StringName", "containsn")
363+
| ("StringName", "is_absolute_path")
364+
| ("StringName", "is_relative_path")
365+
| ("StringName", "simplify_path")
366+
| ("StringName", "get_base_dir")
367+
| ("StringName", "get_file")
368+
| ("StringName", "xml_escape")
369+
| ("StringName", "xml_unescape")
370+
| ("StringName", "uri_encode")
371+
| ("StringName", "uri_decode")
372+
| ("StringName", "c_escape")
373+
| ("StringName", "c_unescape")
374+
| ("StringName", "json_escape")
375+
| ("StringName", "validate_node_name")
376+
| ("StringName", "validate_filename")
377+
| ("StringName", "is_valid_identifier")
378+
| ("StringName", "is_valid_int")
379+
| ("StringName", "is_valid_float")
380+
| ("StringName", "is_valid_hex_number")
381+
| ("StringName", "is_valid_html_color")
382+
| ("StringName", "is_valid_ip_address")
383+
| ("StringName", "is_valid_filename")
384+
| ("StringName", "to_int")
385+
| ("StringName", "to_float")
386+
| ("StringName", "hex_to_int")
387+
| ("StringName", "bin_to_int")
388+
| ("StringName", "trim_prefix")
389+
| ("StringName", "trim_suffix")
390+
| ("StringName", "to_ascii_buffer")
391+
| ("StringName", "to_utf8_buffer")
392+
| ("StringName", "to_utf16_buffer")
393+
| ("StringName", "to_utf32_buffer")
394+
| ("StringName", "hex_decode")
395+
| ("StringName", "to_wchar_buffer")
251396

252397
// NodePath
253398

@@ -261,11 +406,11 @@ pub fn is_builtin_method_exposed(builtin_ty: &TyName, godot_method_name: &str) -
261406

262407
// (add more builtin types below)
263408

264-
// Vector2i
265-
| ("Vector2i", "clampi")
266-
| ("Vector2i", "distance_squared_to")
267-
| ("Vector2i", "distance_to")
268-
| ("Vector2i", "maxi")
409+
// Vector2i
410+
| ("Vector2i", "clampi")
411+
| ("Vector2i", "distance_squared_to")
412+
| ("Vector2i", "distance_to")
413+
| ("Vector2i", "maxi")
269414
| ("Vector2i", "mini")
270415
| ("Vector2i", "snappedi")
271416

godot-core/src/builtin/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ pub mod iter {
8282

8383
/// Specialized types related to Godot's various string implementations.
8484
pub mod strings {
85-
pub use super::string::TransientStringNameOrd;
85+
pub use super::string::{
86+
ExGStringFind, ExGStringSplit, ExStringNameFind, ExStringNameSplit, TransientStringNameOrd,
87+
};
8688
}
8789

8890
// ----------------------------------------------------------------------------------------------------------------------------------------------

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

+26-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
use std::convert::Infallible;
9+
use std::ffi::c_char;
10+
use std::fmt;
811
use std::fmt::Write;
9-
use std::{convert::Infallible, ffi::c_char, fmt, str::FromStr};
1012

1113
use godot_ffi as sys;
1214
use sys::types::OpaqueString;
1315
use sys::{ffi_methods, interface_fn, GodotFfi};
1416

15-
use crate::builtin::{inner, NodePath, StringName};
17+
use crate::builtin::{inner, NodePath, StringName, Variant};
18+
use crate::meta::AsArg;
19+
use crate::{impl_shared_string_api, meta};
1620

1721
/// Godot's reference counted string type.
1822
///
@@ -56,6 +60,10 @@ use crate::builtin::{inner, NodePath, StringName};
5660
/// | General purpose | **`GString`** |
5761
/// | Interned names | [`StringName`][crate::builtin::StringName] |
5862
/// | Scene-node paths | [`NodePath`][crate::builtin::NodePath] |
63+
///
64+
/// # Godot docs
65+
///
66+
/// [`String` (stable)](https://docs.godotengine.org/en/stable/classes/class_string.html)
5967
#[doc(alias = "String")]
6068
// #[repr] is needed on GString itself rather than the opaque field, because PackedStringArray::as_slice() relies on a packed representation.
6169
#[repr(transparent)]
@@ -64,19 +72,19 @@ pub struct GString {
6472
}
6573

6674
impl GString {
67-
/// Construct a new empty GString.
75+
/// Construct a new empty `GString`.
6876
pub fn new() -> Self {
6977
Self::default()
7078
}
7179

80+
/// Number of characters in the string.
81+
///
82+
/// _Godot equivalent: `length`_
83+
#[doc(alias = "length")]
7284
pub fn len(&self) -> usize {
7385
self.as_inner().length().try_into().unwrap()
7486
}
7587

76-
pub fn is_empty(&self) -> bool {
77-
self.as_inner().is_empty()
78-
}
79-
8088
/// Returns a 32-bit integer hash value representing the string.
8189
pub fn hash(&self) -> u32 {
8290
self.as_inner()
@@ -85,7 +93,7 @@ impl GString {
8593
.expect("Godot hashes are uint32_t")
8694
}
8795

88-
/// Gets the UTF-32 character slice from a [`GString`].
96+
/// Gets the UTF-32 character slice from a `GString`.
8997
pub fn chars(&self) -> &[char] {
9098
// SAFETY: Godot 4.1 ensures valid UTF-32, making interpreting as char slice safe.
9199
// See https://github.com/godotengine/godot/pull/74760.
@@ -94,7 +102,7 @@ impl GString {
94102
let len = interface_fn!(string_to_utf32_chars)(s, std::ptr::null_mut(), 0);
95103
let ptr = interface_fn!(string_operator_index_const)(s, 0);
96104

97-
// Even when len == 0, from_raw_parts requires ptr != 0
105+
// Even when len == 0, from_raw_parts requires ptr != null.
98106
if ptr.is_null() {
99107
return &[];
100108
}
@@ -151,7 +159,7 @@ impl GString {
151159
self.move_return_ptr(dst, sys::PtrcallType::Standard);
152160
}
153161

154-
crate::meta::declare_arg_method! {
162+
meta::declare_arg_method! {
155163
/// Use as argument for an [`impl AsArg<StringName|NodePath>`][crate::meta::AsArg] parameter.
156164
///
157165
/// This is a convenient way to convert arguments of similar string types.
@@ -192,7 +200,7 @@ unsafe impl GodotFfi for GString {
192200
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
193201
}
194202

195-
crate::meta::impl_godot_as_self!(GString);
203+
meta::impl_godot_as_self!(GString);
196204

197205
impl_builtin_traits! {
198206
for GString {
@@ -205,6 +213,12 @@ impl_builtin_traits! {
205213
}
206214
}
207215

216+
impl_shared_string_api! {
217+
builtin: GString,
218+
find_builder: ExGStringFind,
219+
split_builder: ExGStringSplit,
220+
}
221+
208222
impl fmt::Display for GString {
209223
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210224
for ch in self.chars() {
@@ -301,7 +315,7 @@ impl From<GString> for String {
301315
}
302316
}
303317

304-
impl FromStr for GString {
318+
impl std::str::FromStr for GString {
305319
type Err = Infallible;
306320

307321
fn from_str(s: &str) -> Result<Self, Self::Err> {

0 commit comments

Comments
 (0)