Skip to content

Commit 3459890

Browse files
committed
fix: resolve char_at index
1 parent 296e63d commit 3459890

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/builtin_function/strings/char_at.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ pub fn char_at(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()>
2020
}
2121
};
2222

23-
// Convert usize index to i32 for bounds checking
24-
if index < 0 {
23+
// Chap uses 1-based indexing, so index must be >= 1
24+
if index < 1 {
2525
return Err(ChapError::runtime_with_msg(
2626
executable.line_number,
2727
format!(
28-
"Index {} is negative. Index must be non-negative",
28+
"Index {} is invalid. Index must be 1 or greater (1-based indexing)",
2929
index
3030
),
3131
));
3232
}
3333

34-
let index = index as usize;
34+
let index_usize = index as usize;
3535

36-
// Check if index is within bounds
37-
if index >= string_value.len() {
36+
// Check if index is within bounds (convert to 0-based for length check)
37+
if index_usize > string_value.len() {
3838
return Err(ChapError::runtime_with_msg(
3939
executable.line_number,
4040
format!(
@@ -45,9 +45,9 @@ pub fn char_at(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()>
4545
));
4646
}
4747

48-
// Get the character at the specified index
48+
// Get the character at the specified index (convert to 0-based indexing)
4949
let chars: Vec<char> = string_value.chars().collect();
50-
let result_char = chars[index];
50+
let result_char = chars[index_usize - 1];
5151
let result = DataType::String(result_char.to_string());
5252

5353
returns(runtime, executable, result)

0 commit comments

Comments
 (0)