MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / ord

Function ord

aiscript-vm/src/builtins/convert.rs:149–178  ·  view source on GitHub ↗
(
    _state: &mut State<'gc>,
    args: Vec<Value<'gc>>,
)

Source from the content-addressed store, hash-verified

147}
148
149pub(super) fn ord<'gc>(
150 _state: &mut State<'gc>,
151 args: Vec<Value<'gc>>,
152) -> Result<Value<'gc>, VmError> {
153 if args.len() != 1 {
154 return Err(VmError::RuntimeError(
155 "ord() takes exactly one argument.".into(),
156 ));
157 }
158
159 let s = match &args[0] {
160 Value::String(s) /*| Value::IoString(s)*/ => s.to_string(),
161 _ => {
162 return Err(VmError::RuntimeError(
163 "ord() argument must be a string.".into(),
164 ))
165 }
166 };
167
168 let mut chars = s.chars();
169 match (chars.next(), chars.next()) {
170 (Some(c), None) => Ok(Value::Number(c as u32 as f64)),
171 (None, _) => Err(VmError::RuntimeError(
172 "ord() argument must be a string of length 1.".into(),
173 )),
174 (Some(_), Some(_)) => Err(VmError::RuntimeError(
175 "ord() argument must be a string of length 1.".into(),
176 )),
177 }
178}
179
180pub(super) fn str<'gc>(
181 state: &mut State<'gc>,

Callers

nothing calls this directly

Calls 3

RuntimeErrorClass · 0.85
lenMethod · 0.80
nextMethod · 0.45

Tested by

no test coverage detected