From 880a5eaad9dcdfd31563385f11ba1f63d38cfd14 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 9 Aug 2026 06:19:30 +0900 Subject: refactor(php-shim): use std::path::MAIN_SEPARATOR over a shim constant The shim's DIRECTORY_SEPARATOR was hardcoded to "/", so every ported `'\\' === DIRECTORY_SEPARATOR` check compared against a constant that does not track the target platform. std::path::MAIN_SEPARATOR and MAIN_SEPARATOR_STR carry the same meaning as PHP's constant and resolve per platform, so the Windows branches are selected on Windows targets. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/autoload/class_loader.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'crates/shirabe/src/autoload') diff --git a/crates/shirabe/src/autoload/class_loader.rs b/crates/shirabe/src/autoload/class_loader.rs index 47b6ee4c..21473fb9 100644 --- a/crates/shirabe/src/autoload/class_loader.rs +++ b/crates/shirabe/src/autoload/class_loader.rs @@ -2,9 +2,8 @@ use indexmap::IndexMap; use shirabe_php_shim::{ - DIRECTORY_SEPARATOR, InvalidArgumentException, PhpMixed, defined, file_exists, include_file, - spl_autoload_register, spl_autoload_unregister, stream_resolve_include_path, strlen, strpos, - strrpos, strtr, substr, + InvalidArgumentException, PhpMixed, defined, file_exists, include_file, spl_autoload_register, + spl_autoload_unregister, stream_resolve_include_path, strlen, strpos, strrpos, strtr, substr, }; use std::sync::{LazyLock, Mutex}; @@ -366,7 +365,11 @@ impl ClassLoader { fn find_file_with_extension(&self, class: &str, ext: &str) -> Option { // PSR-4 lookup - let logical_path_psr4 = format!("{}{}", strtr(class, "\\", DIRECTORY_SEPARATOR), ext); + let logical_path_psr4 = format!( + "{}{}", + strtr(class, "\\", std::path::MAIN_SEPARATOR_STR), + ext + ); let first = class.chars().next().unwrap_or('\0').to_string(); if self.prefix_lengths_psr4.contains_key(&first) { @@ -382,7 +385,7 @@ impl ClassLoader { if let Some(dirs) = self.prefix_dirs_psr4.get(&search) { let path_end = format!( "{}{}", - DIRECTORY_SEPARATOR, + std::path::MAIN_SEPARATOR, substr(&logical_path_psr4, (last_pos + 1) as i64, None) ); for dir in dirs { @@ -397,7 +400,7 @@ impl ClassLoader { // PSR-4 fallback dirs for dir in &self.fallback_dirs_psr4 { - let file = format!("{}{}{}", dir, DIRECTORY_SEPARATOR, logical_path_psr4); + let file = format!("{}{}{}", dir, std::path::MAIN_SEPARATOR, logical_path_psr4); if file_exists(&file) { return Some(file); } @@ -413,19 +416,24 @@ impl ClassLoader { strtr( &substr(&logical_path_psr4, (pos + 1) as i64, None), "_", - DIRECTORY_SEPARATOR + std::path::MAIN_SEPARATOR_STR, ) ); } else { // PEAR-like class name - logical_path_psr0 = format!("{}{}", strtr(class, "_", DIRECTORY_SEPARATOR), ext); + logical_path_psr0 = format!( + "{}{}", + strtr(class, "_", std::path::MAIN_SEPARATOR_STR), + ext + ); } if let Some(prefixes) = self.prefixes_psr0.get(&first) { for (prefix, dirs) in prefixes { if Some(0) == strpos(class, prefix) { for dir in dirs { - let file = format!("{}{}{}", dir, DIRECTORY_SEPARATOR, logical_path_psr0); + let file = + format!("{}{}{}", dir, std::path::MAIN_SEPARATOR, logical_path_psr0); if file_exists(&file) { return Some(file); } @@ -436,7 +444,7 @@ impl ClassLoader { // PSR-0 fallback dirs for dir in &self.fallback_dirs_psr0 { - let file = format!("{}{}{}", dir, DIRECTORY_SEPARATOR, logical_path_psr0); + let file = format!("{}{}{}", dir, std::path::MAIN_SEPARATOR, logical_path_psr0); if file_exists(&file) { return Some(file); } -- cgit v1.3.1-4-g156e