config: centralize NYASH_STR_CP and add cp smoke

This commit is contained in:
2025-12-28 13:44:53 +09:00
parent 10e6a15552
commit fe3eb9f1f7
9 changed files with 90 additions and 8 deletions

View File

@ -107,7 +107,7 @@ impl StringBox {
/// otherwise use UTF-8 byte length (legacy/default).
pub fn length(&self) -> Box<dyn NyashBox> {
use crate::box_trait::IntegerBox;
let use_cp = std::env::var("NYASH_STR_CP").ok().as_deref() == Some("1");
let use_cp = crate::config::env::string_codepoint_mode();
let n = if use_cp {
self.value.chars().count() as i64
} else {
@ -144,10 +144,9 @@ impl StringBox {
/// Get substring from start to end (exclusive)
pub fn substring(&self, start: usize, end: usize) -> Box<dyn NyashBox> {
let chars: Vec<char> = self.value.chars().collect();
let actual_end = end.min(chars.len());
let actual_start = start.min(actual_end);
let substring: String = chars[actual_start..actual_end].iter().collect();
let mode = crate::boxes::string_ops::index_mode_from_env();
let substring =
crate::boxes::string_ops::substring(&self.value, start as i64, Some(end as i64), mode);
Box::new(StringBox::new(substring))
}
}

View File

@ -7,7 +7,7 @@ pub enum StringIndexMode {
}
pub fn index_mode_from_env() -> StringIndexMode {
if std::env::var("NYASH_STR_CP").ok().as_deref() == Some("1") {
if crate::config::env::string_codepoint_mode() {
StringIndexMode::CodePoint
} else {
StringIndexMode::Byte