11// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22
33use crate :: typeref:: { EMPTY_UNICODE , STR_TYPE } ;
4+ #[ cfg( target_endian = "little" ) ]
45use crate :: util:: isize_to_usize;
5-
6+ # [ cfg ( target_endian = "little" ) ]
67use core:: ffi:: c_void;
78use core:: ptr:: NonNull ;
8- use pyo3_ffi:: { PyASCIIObject , PyCompactUnicodeObject , PyObject } ;
9+ #[ cfg( target_endian = "little" ) ]
10+ use pyo3_ffi:: PyCompactUnicodeObject ;
11+ use pyo3_ffi:: { PyASCIIObject , PyObject } ;
912
1013fn to_str_via_ffi ( op : * mut PyObject ) -> Option < & ' static str > {
1114 let mut str_size: pyo3_ffi:: Py_ssize_t = 0 ;
@@ -32,29 +35,21 @@ pub fn set_str_create_fn() {
3235 }
3336}
3437
35- #[ cfg( all( Py_3_14 , Py_GIL_DISABLED ) ) ]
36- const STATE_COMPACT_ASCII : u32 = u32:: from_le ( 0b000000000000000000_0_1_1_001_00000000 ) ;
37-
38- #[ cfg( not( all( Py_3_14 , Py_GIL_DISABLED ) ) ) ]
39- const STATE_COMPACT_ASCII : u32 = u32:: from_le ( 0b000000000000000000000000_0_1_1_001_00 ) ;
40-
41- #[ cfg( all( Py_3_14 , Py_GIL_DISABLED ) ) ]
42- const STATE_COMPACT : u32 = u32:: from_le ( 0b000000000000000000_0_0_1_000_00000000 ) ;
43-
44- #[ cfg( not( all( Py_3_14 , Py_GIL_DISABLED ) ) ) ]
45- const STATE_COMPACT : u32 = u32:: from_le ( 0b000000000000000000000000_0_0_1_000_00 ) ;
38+ #[ cfg( all( target_endian = "little" , Py_3_14 , Py_GIL_DISABLED ) ) ]
39+ const STATE_KIND_SHIFT : usize = 8 ;
4640
47- #[ cfg( all( Py_3_14 , Py_GIL_DISABLED ) ) ]
48- const STATE_KIND_MASK : u32 = u32 :: from_le ( 0b111_00000000 ) ;
41+ #[ cfg( all( target_endian = "little" , not ( all ( Py_3_14 , Py_GIL_DISABLED ) ) ) ) ]
42+ const STATE_KIND_SHIFT : usize = 2 ;
4943
50- #[ cfg( all ( Py_3_14 , Py_GIL_DISABLED ) ) ]
51- const STATE_KIND_SHIFT : usize = 8 ;
44+ #[ cfg( target_endian = "little" ) ]
45+ const STATE_KIND_MASK : u32 = 7 << STATE_KIND_SHIFT ;
5246
53- #[ cfg( not( all( Py_3_14 , Py_GIL_DISABLED ) ) ) ]
54- const STATE_KIND_MASK : u32 = u32:: from_le ( 0b111_00 ) ;
47+ #[ cfg( target_endian = "little" ) ]
48+ const STATE_COMPACT_ASCII : u32 =
49+ 1 << STATE_KIND_SHIFT | 1 << ( STATE_KIND_SHIFT + 3 ) | 1 << ( STATE_KIND_SHIFT + 4 ) ;
5550
56- #[ cfg( not ( all ( Py_3_14 , Py_GIL_DISABLED ) ) ) ]
57- const STATE_KIND_SHIFT : usize = 2 ;
51+ #[ cfg( target_endian = "little" ) ]
52+ const STATE_COMPACT : u32 = 1 << ( STATE_KIND_SHIFT + 3 ) ;
5853
5954#[ repr( transparent) ]
6055#[ derive( Copy , Clone ) ]
@@ -96,6 +91,7 @@ impl PyStr {
9691 }
9792 }
9893
94+ #[ cfg( target_endian = "little" ) ]
9995 pub fn hash ( & mut self ) {
10096 unsafe {
10197 let ptr = self . ptr . as_ptr ( ) . cast :: < PyASCIIObject > ( ) ;
@@ -118,7 +114,24 @@ impl PyStr {
118114 }
119115 }
120116
117+ #[ cfg( not( target_endian = "little" ) ) ]
118+ pub fn hash ( & mut self ) {
119+ unsafe {
120+ let data_ptr = ffi ! ( PyUnicode_DATA ( self . ptr. as_ptr( ) ) ) ;
121+ #[ allow( clippy:: cast_possible_wrap) ]
122+ let num_bytes =
123+ ffi ! ( PyUnicode_KIND ( self . ptr. as_ptr( ) ) ) as isize * ffi ! ( Py_SIZE ( self . ptr. as_ptr( ) ) ) ;
124+ #[ cfg( Py_3_14 ) ]
125+ let hash = pyo3_ffi:: Py_HashBuffer ( data_ptr, num_bytes) ;
126+ #[ cfg( not( Py_3_14 ) ) ]
127+ let hash = pyo3_ffi:: _Py_HashBytes ( data_ptr, num_bytes) ;
128+ ( * self . ptr . as_ptr ( ) . cast :: < PyASCIIObject > ( ) ) . hash = hash;
129+ debug_assert ! ( ( * self . ptr. as_ptr( ) . cast:: <PyASCIIObject >( ) ) . hash != -1 ) ;
130+ }
131+ }
132+
121133 #[ inline( always) ]
134+ #[ cfg( target_endian = "little" ) ]
122135 pub fn to_str ( self ) -> Option < & ' static str > {
123136 unsafe {
124137 let op = self . ptr . as_ptr ( ) ;
@@ -140,6 +153,12 @@ impl PyStr {
140153 }
141154 }
142155
156+ #[ inline( always) ]
157+ #[ cfg( not( target_endian = "little" ) ) ]
158+ pub fn to_str ( self ) -> Option < & ' static str > {
159+ to_str_via_ffi ( self . ptr . as_ptr ( ) )
160+ }
161+
143162 pub fn as_ptr ( self ) -> * mut PyObject {
144163 self . ptr . as_ptr ( )
145164 }
0 commit comments