2017-09-15 16:03:14 -04:00
|
|
|
use std::{slice, str};
|
|
|
|
|
2020-12-12 15:37:17 -05:00
|
|
|
#[cfg(feature = "serialize")]
|
|
|
|
use {
|
|
|
|
serde::ser::{Serialize, Serializer},
|
|
|
|
std::result::Result as StdResult,
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:27:37 -04:00
|
|
|
use crate::error::{Error, Result};
|
|
|
|
use crate::ffi;
|
|
|
|
use crate::types::LuaRef;
|
|
|
|
use crate::util::{assert_stack, StackGuard};
|
2017-09-15 16:03:14 -04:00
|
|
|
|
|
|
|
/// Handle to an internal Lua string.
|
|
|
|
///
|
|
|
|
/// Unlike Rust strings, Lua strings may not be valid UTF-8.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct String<'lua>(pub(crate) LuaRef<'lua>);
|
|
|
|
|
|
|
|
impl<'lua> String<'lua> {
|
|
|
|
/// Get a `&str` slice if the Lua string is valid UTF-8.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-10-17 11:59:33 -04:00
|
|
|
/// # use mlua::{Lua, Result, String};
|
2019-09-27 12:27:37 -04:00
|
|
|
/// # fn main() -> Result<()> {
|
2019-10-17 11:59:33 -04:00
|
|
|
/// # let lua = Lua::new();
|
2017-09-15 16:03:14 -04:00
|
|
|
/// let globals = lua.globals();
|
|
|
|
///
|
|
|
|
/// let version: String = globals.get("_VERSION")?;
|
2019-10-17 11:59:33 -04:00
|
|
|
/// assert!(version.to_str()?.contains("Lua"));
|
2017-09-15 16:03:14 -04:00
|
|
|
///
|
2019-10-17 11:59:33 -04:00
|
|
|
/// let non_utf8: String = lua.load(r#" "test\255" "#).eval()?;
|
2017-09-15 16:03:14 -04:00
|
|
|
/// assert!(non_utf8.to_str().is_err());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn to_str(&self) -> Result<&str> {
|
2017-12-16 17:46:32 -05:00
|
|
|
str::from_utf8(self.as_bytes()).map_err(|e| Error::FromLuaConversionError {
|
|
|
|
from: "string",
|
|
|
|
to: "&str",
|
|
|
|
message: Some(e.to_string()),
|
2017-09-15 16:03:14 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the bytes that make up this string.
|
|
|
|
///
|
2017-09-15 17:33:28 -04:00
|
|
|
/// The returned slice will not contain the terminating nul byte, but will contain any nul
|
2017-09-15 16:03:14 -04:00
|
|
|
/// bytes embedded into the Lua string.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-10-17 11:59:33 -04:00
|
|
|
/// # use mlua::{Lua, Result, String};
|
2019-09-27 12:27:37 -04:00
|
|
|
/// # fn main() -> Result<()> {
|
2019-10-17 11:59:33 -04:00
|
|
|
/// # let lua = Lua::new();
|
|
|
|
/// let non_utf8: String = lua.load(r#" "test\255" "#).eval()?;
|
2017-09-15 16:03:14 -04:00
|
|
|
/// assert!(non_utf8.to_str().is_err()); // oh no :(
|
|
|
|
/// assert_eq!(non_utf8.as_bytes(), &b"test\xff"[..]);
|
2019-09-27 12:27:37 -04:00
|
|
|
/// # Ok(())
|
2017-09-15 16:03:14 -04:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
2017-09-15 17:33:28 -04:00
|
|
|
let nulled = self.as_bytes_with_nul();
|
2017-09-30 00:53:28 -04:00
|
|
|
&nulled[..nulled.len() - 1]
|
2017-09-15 17:33:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the bytes that make up this string, including the trailing nul byte.
|
|
|
|
pub fn as_bytes_with_nul(&self) -> &[u8] {
|
2017-09-15 16:03:14 -04:00
|
|
|
let lua = self.0.lua;
|
|
|
|
unsafe {
|
2018-03-12 13:13:44 -04:00
|
|
|
let _sg = StackGuard::new(lua.state);
|
2018-03-19 15:26:21 -04:00
|
|
|
assert_stack(lua.state, 1);
|
2017-09-15 16:03:14 -04:00
|
|
|
|
2018-03-12 13:13:44 -04:00
|
|
|
lua.push_ref(&self.0);
|
2019-10-01 11:11:12 -04:00
|
|
|
mlua_debug_assert!(
|
2018-03-12 13:13:44 -04:00
|
|
|
ffi::lua_type(lua.state, -1) == ffi::LUA_TSTRING,
|
|
|
|
"string ref is not string type"
|
|
|
|
);
|
2017-09-15 16:03:14 -04:00
|
|
|
|
2018-03-12 13:13:44 -04:00
|
|
|
let mut size = 0;
|
|
|
|
// This will not trigger a 'm' error, because the reference is guaranteed to be of
|
|
|
|
// string type
|
|
|
|
let data = ffi::lua_tolstring(lua.state, -1, &mut size);
|
|
|
|
|
|
|
|
slice::from_raw_parts(data as *const u8, size + 1)
|
2017-09-15 16:03:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 17:10:53 -04:00
|
|
|
|
|
|
|
impl<'lua> AsRef<[u8]> for String<'lua> {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
self.as_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua strings are basically &[u8] slices, so implement PartialEq for anything resembling that.
|
|
|
|
//
|
2019-10-01 11:11:12 -04:00
|
|
|
// This makes our `String` comparable with `Vec<u8>`, `[u8]`, `&str`, `String` and `mlua::String`
|
2017-09-15 17:10:53 -04:00
|
|
|
// itself.
|
|
|
|
//
|
|
|
|
// The only downside is that this disallows a comparison with `Cow<str>`, as that only implements
|
|
|
|
// `AsRef<str>`, which collides with this impl. Requiring `AsRef<str>` would fix that, but limit us
|
|
|
|
// in other ways.
|
2017-09-30 00:53:28 -04:00
|
|
|
impl<'lua, T> PartialEq<T> for String<'lua>
|
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
2017-09-15 17:10:53 -04:00
|
|
|
fn eq(&self, other: &T) -> bool {
|
|
|
|
self.as_bytes() == other.as_ref()
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 15:37:17 -05:00
|
|
|
|
|
|
|
#[cfg(feature = "serialize")]
|
|
|
|
impl<'lua> Serialize for String<'lua> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match self.to_str() {
|
|
|
|
Ok(s) => serializer.serialize_str(s),
|
|
|
|
Err(_) => serializer.serialize_bytes(self.as_bytes()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|