il2cpp_bridge_rs/structs/collections/string.rs
1//! IL2CPP String definition and operations
2use crate::api;
3use std::os::raw::c_void;
4use std::slice;
5
6/// Represents an IL2CPP String
7#[repr(C)]
8#[derive(Debug, Clone)]
9pub struct Il2cppString {
10 /// Pointer to the string class
11 pub klass: *mut c_void,
12 /// Monitor for synchronization
13 pub monitor: *mut c_void,
14 /// Length of the string (in characters, not bytes)
15 pub length: i32,
16 /// Start of the character array (UTF-16)
17 pub chars: [u16; 1],
18}
19
20impl Il2cppString {
21 /// Converts the Il2cppString to a Rust String
22 ///
23 /// # Returns
24 /// * `Option<String>` - The converted Rust string, or None if invalid UTF-16
25 pub fn to_string(&self) -> Option<String> {
26 let len = self.length as usize;
27 unsafe {
28 let slice = slice::from_raw_parts(self.chars.as_ptr(), len);
29 String::from_utf16(slice).ok()
30 }
31 }
32
33 /// Creates a new Il2cppString from a Rust string slice
34 ///
35 /// # Arguments
36 /// * `s` - The Rust string slice to convert
37 ///
38 /// # Returns
39 /// * `*mut Il2cppString` - Pointer to the newly created IL2CPP string
40 pub fn new(s: &str) -> *mut Il2cppString {
41 let c_str = std::ffi::CString::new(s).unwrap();
42 unsafe { api::string_new(c_str.as_ptr()) as *mut Il2cppString }
43 }
44}