File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -221,19 +221,6 @@ pub fn to_ascii(value: &str) -> AsciiString {
221221 unsafe { AsciiString :: from_ascii_unchecked ( ascii) }
222222}
223223
224- #[ doc( hidden) ]
225- pub const fn bytes_is_ascii ( x : & str ) -> bool {
226- let x = x. as_bytes ( ) ;
227- let mut i = 0 ;
228- while i < x. len ( ) {
229- if !x[ i] . is_ascii ( ) {
230- return false ;
231- }
232- i += 1 ;
233- }
234- true
235- }
236-
237224pub mod levenshtein {
238225 use std:: { cell:: RefCell , thread_local} ;
239226
@@ -335,15 +322,24 @@ pub mod levenshtein {
335322 }
336323}
337324
325+ /// Creates an [`AsciiStr`][ascii::AsciiStr] from a string literal, throwing a compile error if the
326+ /// literal isn't actually ascii.
327+ ///
328+ /// ```compile_fail
329+ /// # use rustpython_common::str::ascii;
330+ /// ascii!("I ❤️ Rust & Python");
331+ /// ```
338332#[ macro_export]
339333macro_rules! ascii {
340334 ( $x: literal) => { {
341- const _: ( ) = {
342- [ "not ascii" ] [ !$crate:: str :: bytes_is_ascii( $x) as usize ] ;
335+ const STR : & str = $x;
336+ const _: ( ) = if !STR . is_ascii( ) {
337+ panic!( "ascii!() argument is not an ascii string" ) ;
343338 } ;
344- unsafe { $crate:: vendored:: ascii:: AsciiStr :: from_ascii_unchecked( $x . as_bytes( ) ) }
339+ unsafe { $crate:: vendored:: ascii:: AsciiStr :: from_ascii_unchecked( STR . as_bytes( ) ) }
345340 } } ;
346341}
342+ pub use ascii;
347343
348344#[ cfg( test) ]
349345mod tests {
Original file line number Diff line number Diff line change @@ -75,15 +75,8 @@ impl OsPath {
7575 Path :: new ( & self . path )
7676 }
7777
78- #[ cfg( any( unix, target_os = "wasi" ) ) ]
7978 pub fn into_bytes ( self ) -> Vec < u8 > {
80- use rustpython_common:: os:: ffi:: OsStrExt ;
81- self . path . as_bytes ( ) . to_vec ( )
82- }
83-
84- #[ cfg( windows) ]
85- pub fn into_bytes ( self ) -> Vec < u8 > {
86- self . path . to_string_lossy ( ) . to_string ( ) . into_bytes ( )
79+ self . path . into_encoded_bytes ( )
8780 }
8881
8982 pub fn into_cstring ( self , vm : & VirtualMachine ) -> PyResult < ffi:: CString > {
You can’t perform that action at this time.
0 commit comments