Skip to main content

low_luajit/lua/type/
string.rs

1use core::{ffi::CStr, ptr, slice, str};
2use crate::{AmountReturned, LuaState, ffi, map_ptr};
3
4/// String related functions.
5impl LuaState {
6	/// Returns `true` if the value at the given acceptable `index` is a string (or number convertible to string), `false` otherwise.
7	#[inline]
8	#[must_use]
9	pub fn is_string(&self, index: i32) -> bool {
10		// SAFETY: self.0 is always valid.
11		unsafe { ffi::lua_isstring(self.0, index) != 0 }
12	}
13
14	/// Converts the value at the given acceptable `index` to a string and returns it as a string slice.
15	///
16	/// Only valid UTF-8 strings or numbers are accepted. Anything else will return `None`.
17	///
18	/// This function *does* affect the element in the stack itself:
19	/// numbers will be replaced by the converted string of said number.
20	///
21	/// To instead obtain a printable representation of any value, see [`LuaState::display`].
22	#[inline]
23	#[must_use]
24	pub fn to_string(&self, index: i32) -> Option<&str> {
25		str::from_utf8(self.to_byte_string(index)?).ok()
26	}
27
28	/// Indices that are not positive, not a pseudo-index, and go out of bounds may convert a number that's used elsewhere to a string.
29	/// This conversion usually leads to UB soon after, which is pretty bad.
30	///
31	/// This function checks if the index is invalid in this specific context and panics if it is.
32	///
33	/// As this panic is very niche, it is currently undocumented. This may change in the future.
34	#[inline]
35	pub(crate) fn assert_string_index(&self, index: i32) {
36		assert!(
37			index.is_positive() || index <= Self::REGISTRY_INDEX || self.is_index_valid(index) || !self.is_number(index),
38			concat!(
39				"cannot convert the number at index {} to a string, as there are only {} elements\n",
40				"(converting this out of bounds number would very likely cause UB)"
41			),
42			index,
43			self.get_top()
44		);
45	}
46
47	/// Converts the value at the given acceptable `index` to a string and returns it as a byte slice.
48	///
49	/// Only strings or numbers are accepted. Anything else will return `None`.
50	///
51	/// This function *does* affect the element in the stack itself:
52	/// numbers will be replaced by the converted string of said number.
53	#[inline]
54	#[must_use]
55	pub fn to_byte_string(&self, index: i32) -> Option<&[u8]> {
56		self.assert_string_index(index);
57
58		// SAFETY: self.0 is always valid, len is initialized right before passing its pointer,
59		// and lua_tolstring returns valid for len amount of values string pointers when not null.
60		let mut len = 0;
61		let string = unsafe { ffi::lua_tolstring(self.0, index, &raw mut len) };
62		map_ptr!(string, unsafe { slice::from_raw_parts(string.cast(), len) })
63	}
64
65	/// Converts the value at the given acceptable `index` to a string and returns it as a `CStr`.
66	///
67	/// Only strings or numbers are accepted. Anything else will return `None`.
68	///
69	/// This function *does* affect the element in the stack itself:
70	/// numbers will be replaced by the converted string of said number.
71	#[inline]
72	#[must_use]
73	pub fn to_cstring(&self, index: i32) -> Option<&CStr> {
74		self.assert_string_index(index);
75
76		// SAFETY: self.0 is always valid and lua_tostring returns valid nul-terminated string pointers when not null.
77		let cstring = unsafe { ffi::lua_tostring(self.0, index) };
78		map_ptr!(cstring, unsafe { CStr::from_ptr(cstring) })
79	}
80
81	/// Pushes a copy of the given `string` onto the stack.
82	#[inline]
83	pub fn push_string(&mut self, string: impl AsRef<[u8]>) {
84		self.push_byte_string(string.as_ref());
85	}
86
87	/// Pushes a copy of the given `byte_string` onto the stack.
88	#[inline]
89	pub fn push_byte_string(&mut self, byte_string: &[u8]) {
90		// SAFETY: self.0is always valid, the pointer remains valid for the duration of this function,
91		// and the length is correct as it's returned from the array itself.
92		unsafe { ffi::lua_pushlstring(self.0, byte_string.as_ptr().cast(), byte_string.len()); }
93	}
94
95	/// Pushes a copy of the given `cstring` onto the stack.
96	///
97	/// If `cstring` is `None`, `nil` will be pushed onto the stack instead.
98	#[inline]
99	pub fn push_cstring<'a>(&mut self, cstring: impl Into<Option<&'a CStr>>) {
100		// SAFETY: self.0 is always valid and the pointer either remains valid for the duration of this function
101		// or is null which lua_pushstring accepts.
102		unsafe { ffi::lua_pushstring(self.0, cstring.into().map_or(core::ptr::null(), CStr::as_ptr)); }
103	}
104
105	/// Checks if there's a valid UTF-8 string (or number convertible to string) at the given index `arg_n` and returns it as a string slice.
106	///
107	/// If the check fails, [`LuaState::type_error`] is called using `arg_n`.
108	///
109	/// This function has the same caveats as [`LuaState::to_string`].
110	///
111	/// # Lua Errors
112	///
113	/// Intentionally errors if the check fails.
114	#[inline]
115	pub fn check_string(&self, arg_n: i32) -> &str {
116		match str::from_utf8(self.check_byte_string(arg_n)) {
117			Ok(string) => string,
118			Err(_) => self.arg_error(arg_n, c"string contains invalid UTF-8"),
119		}
120	}
121
122	/// Checks if there's a string (or number convertible to string) at the given index `arg_n` and returns it as a byte slice.
123	///
124	/// If the check fails, [`LuaState::type_error`] is called using `arg_n`.
125	///
126	/// This function has the same caveats as [`LuaState::to_cstring`].
127	///
128	/// # Lua Errors
129	///
130	/// Intentionally errors if the check fails.
131	#[inline]
132	pub fn check_byte_string(&self, arg_n: i32) -> &[u8] {
133		self.assert_string_index(arg_n);
134
135		// SAFETY: self.0 is always valid, len is initialized right before passing its pointer,
136		// and string should always be non-null and valid for len amount of values.
137		let mut len = 0;
138		let string = unsafe { ffi::luaL_checklstring(self.0, arg_n, &raw mut len) };
139		unsafe { slice::from_raw_parts(string.cast(), len) }
140	}
141
142	/// Checks if there's a string (or number convertible to string) at the given index `arg_n` and returns it as a `CStr`.
143	///
144	/// If the check fails, [`LuaState::type_error`] is called using `arg_n`.
145	///
146	/// This function has the same caveats as [`LuaState::to_byte_string`].
147	///
148	/// # Lua Errors
149	///
150	/// Intentionally errors if the check fails.
151	#[inline]
152	pub fn check_cstring(&self, arg_n: i32) -> &CStr {
153		self.assert_string_index(arg_n);
154
155		// SAFETY: self.0 is always valid and the returned pointer should be non-null, nul terminated, and valid for all values.
156		unsafe { CStr::from_ptr(ffi::luaL_checkstring(self.0, arg_n)) }
157	}
158
159	/// Behaves exactly as [`LuaState::check_string`],
160	/// but returns `default` instead of erroring when the element at the given index `arg_n` is `nil` (or non-existing).
161	#[inline]
162	pub fn opt_string<'a>(&'a self, arg_n: i32, default: &'a str) -> &'a str {
163		self.assert_string_index(arg_n);
164
165		// SAFETY: self.0 is always valid, luaL_optlstring accepts a null pointer for the default value,
166		// and len is initialized right before passing its pointer.
167		let mut len = 0;
168		let string = unsafe { ffi::luaL_optlstring(self.0, arg_n, ptr::null(), &raw mut len) };
169
170		if string.is_null() {
171			return default;
172		}
173
174		// SAFETY: string should always be non-null and valid for len amount of values.
175		match str::from_utf8(unsafe { slice::from_raw_parts(string.cast(), len) }) {
176			Ok(string) => string,
177			Err(_) => self.arg_error(arg_n, c"string contains invalid UTF-8"),
178		}
179	}
180
181	/// Behaves exactly as [`LuaState::check_byte_string`],
182	/// but returns `default` instead of erroring when the element at the given index `arg_n` is `nil` (or non-existing).
183	#[inline]
184	pub fn opt_byte_string<'a>(&'a self, arg_n: i32, default: &'a CStr) -> &'a [u8] {
185		self.assert_string_index(arg_n);
186
187		// SAFETY: self.0 and default are always valid, len is initialized right before passing its pointer,
188		// and string should always be non-null and valid for len amount of values.
189		let mut len = 0;
190		let string = unsafe { ffi::luaL_optlstring(self.0, arg_n, default.as_ptr(), &raw mut len) };
191		unsafe { slice::from_raw_parts(string.cast(), len) }
192	}
193
194	/// Behaves exactly as [`LuaState::check_cstring`],
195	/// but returns `default` instead of erroring when the element at the given index `arg_n` is `nil` (or non-existing).
196	#[inline]
197	pub fn opt_cstring<'a>(&'a self, arg_n: i32, default: &'a CStr) -> &'a CStr {
198		self.assert_string_index(arg_n);
199
200		// SAFETY: self.0 and default are always valid, len is initialized right before passing its pointer,
201		// and string should always be non-null and valid for len amount of values.
202		unsafe { CStr::from_ptr(ffi::luaL_optstring(self.0, arg_n, default.as_ptr())) }
203	}
204
205	/// Finishes the currently called [`CFunction`], returning the given `string` to the caller.
206	///
207	/// The `LuaState` wrapper is consumed to prevent invalidating the `AmountReturned` (the Lua state itself is left intact).
208	///
209	/// [`CFunction`]: ./type.CFunction.html
210	#[inline]
211	pub fn return_string(self, string: impl AsRef<[u8]>) -> AmountReturned {
212		self.return_byte_string(string.as_ref())
213	}
214
215	/// Finishes the currently called [`CFunction`], returning the given `byte_string` to the caller.
216	///
217	/// The `LuaState` wrapper is consumed to prevent invalidating the `AmountReturned` (the Lua state itself is left intact).
218	///
219	/// [`CFunction`]: ./type.CFunction.html
220	#[inline]
221	pub fn return_byte_string(mut self, byte_string: &[u8]) -> AmountReturned {
222		self.push_byte_string(byte_string);
223
224		// SAFETY: There should always be at least one element in the stack.
225		unsafe { AmountReturned::unchecked(1) }
226	}
227
228	/// Finishes the currently called [`CFunction`], returning the given `cstring` to the caller.
229	///
230	/// If `cstring` is `None`, `nil` will be returned to the caller instead.
231	///
232	/// The `LuaState` wrapper is consumed to prevent invalidating the `AmountReturned` (the Lua state itself is left intact).
233	///
234	/// [`CFunction`]: ./type.CFunction.html
235	#[inline]
236	pub fn return_cstring<'a>(mut self, cstring: impl Into<Option<&'a CStr>>) -> AmountReturned {
237		self.push_cstring(cstring);
238
239		// SAFETY: There should always be at least one element in the stack.
240		unsafe { AmountReturned::unchecked(1) }
241	}
242
243	/// Creates a copy of `string` where every occurrence of `pattern` is replaced by `replacement`.
244	///
245	/// The resulting string is both pushed onto the stack and returned.
246	#[inline]
247	pub fn gsub(&mut self, string: &CStr, pattern: &CStr, replacement: &CStr) -> &CStr {
248		// SAFETY: self.0 and string, pattern, and replacement's pointers are always valid and luaL_gsub always returns a valid string pointer.
249		unsafe { CStr::from_ptr(ffi::luaL_gsub(self.0, string.as_ptr(), pattern.as_ptr(), replacement.as_ptr())) }
250	}
251}