789789first (str:: Str , n:: Integer ) = str[1 : min (end , nextind (str, 0 , n))]
790790last (str:: Str , n:: Integer ) = str[max (1 , prevind (str, ncodeunits (str)+ 1 , n)): end ]
791791
792- const HAS_WMEMCPY = ! (@static V6_COMPAT ? is_windows () : Sys. iswindows ())
793-
794792const Chrs = @static V6_COMPAT ? Union{Char,AbstractChar} : Chr
795793
796794function repeat (ch:: CP , cnt:: Integer ) where {CP <: Chrs }
@@ -804,23 +802,37 @@ end
804802
805803# low level mem support functions
806804
805+ const HAS_WMEM = ! (@static V6_COMPAT ? is_windows () : Sys. iswindows ())
806+
807+ @static if HAS_WMEM
808+
807809const (WidChr,OthChr) = @static sizeof (Cwchar_t) == 4 ? (UInt32,UInt16) : (UInt16,UInt32)
808810
809- _fwd_memchr (ptr:: Ptr{UInt8} , byt:: UInt8 , len:: Integer ) =
810- ccall (:memchr , Ptr{UInt8}, (Ptr{UInt8}, Int32, Csize_t), ptr, byt, len)
811+ @inline _memcmp (a:: Ptr{T} , b:: Ptr{T} , len) where {T<: WidChr } =
812+ ccall (:wmemcmp , Int32, (Ptr{T}, Ptr{T}, UInt), a, b, len)
813+ @inline _memcpy (a:: Ptr{T} , b:: Ptr{T} , len) where {T<: WidChr } =
814+ ccall (:wmemcpy , Ptr{T}, (Ptr{T}, Ptr{T}, UInt), a, b, len)
815+ @inline _memset (pnt:: Ptr{T} , ch:: T , cnt) where {T<: WidChr } =
816+ ccall (:wmemset , Ptr{T}, (Ptr{T}, Cuint, Csize_t), pnt, ch, cnt)
811817
812- _fwd_memchr (ptr:: Ptr{WidChr} , wchr:: WidChr , len:: Integer ) =
813- ccall (:wmemchr , Ptr{WidChr}, (Ptr{WidChr}, Int32, Csize_t), ptr, wchr, len)
818+ _fwd_memchr (ptr:: Ptr{T} , wchr:: T , len:: Integer ) where {T<: WidChr } =
819+ ccall (:wmemchr , Ptr{T}, (Ptr{T}, Int32, Csize_t), ptr, wchr, len)
820+ _fwd_memchr (ptr:: Ptr{T} , wchr:: T , fin:: Ptr{T} ) where {T<: WidChr } =
821+ ptr < fin ? _fwd_memchr (ptr, wchr, chroff (fin - ptr)) : C_NULL
822+
823+ else
824+ const OthChr = Union{UInt16, UInt32}
825+ end
814826
815- _fwd_memchr (beg:: Ptr{OthChr} , wchr:: OthChr , len:: Integer ) =
827+ _fwd_memchr (ptr:: Ptr{T} , byt:: T , len:: Integer ) where {T<: UInt8 } =
828+ ccall (:memchr , Ptr{T}, (Ptr{T}, Int32, Csize_t), ptr, byt, len)
829+
830+ _fwd_memchr (beg:: Ptr{T} , wchr:: T , len:: Integer ) where {T<: OthChr } =
816831 _fwd_memchr (beg, ch, bytoff (beg, len))
817832
818- _fwd_memchr (ptr:: Ptr{UInt8 } , byt:: UInt8 , fin:: Ptr{UInt8} ) =
833+ _fwd_memchr (ptr:: Ptr{T } , byt:: T , fin:: Ptr{T} ) where {T <: UInt8 } =
819834 ptr < fin ? _fwd_memchr (ptr, byt, fin - ptr) : C_NULL
820835
821- _fwd_memchr (ptr:: Ptr{WidChr} , wchr:: WidChr , fin:: Ptr{WidChr} ) =
822- ptr < fin ? _fwd_memchr (ptr, wchr, chroff (fin - ptr)) : C_NULL
823-
824836function _fwd_memchr (pnt:: Ptr{T} , wchr:: T , fin:: Ptr{T} ) where {T<: OthChr }
825837 while pnt < fin
826838 get_codeunit (pnt) == ch && return pnt
@@ -829,8 +841,8 @@ function _fwd_memchr(pnt::Ptr{T}, wchr::T, fin::Ptr{T}) where {T<:OthChr}
829841 C_NULL
830842end
831843
832- _rev_memchr (ptr:: Ptr{UInt8 } , byt:: UInt8 , len:: Integer ) =
833- ccall (:memrchr , Ptr{UInt8 }, (Ptr{UInt8 }, Int32, Csize_t), ptr, byt, len)
844+ _rev_memchr (ptr:: Ptr{T } , byt:: T , len:: Integer ) where {T <: UInt8 } =
845+ ccall (:memrchr , Ptr{T }, (Ptr{T }, Int32, Csize_t), ptr, byt, len)
834846
835847function _rev_memchr (beg:: Ptr{T} , ch:: T , len:: Integer ) where {T<: Union{UInt16,UInt32} }
836848 pnt = bytoff (beg, len)
@@ -840,17 +852,15 @@ function _rev_memchr(beg::Ptr{T}, ch::T, len::Integer) where {T<:Union{UInt16,UI
840852 C_NULL
841853end
842854
843- _memcmp (a:: Ptr{UInt8} , b:: Ptr{UInt8} , len) =
844- ccall (:memcmp , Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, len)
845- _memcmp (a:: Ptr{WidChr} , b:: Ptr{WidChr} , len) =
846- ccall (:wmemcmp , Int32, (Ptr{WidChr}, Ptr{WidChr}, UInt), a, b, len)
855+ _memcmp (a:: Ptr{T} , b:: Ptr{T} , len) where {T<: UInt8 } =
856+ ccall (:memcmp , Int32, (Ptr{T}, Ptr{T}, UInt), a, b, len)
847857
848- function _memcmp (apnt:: Ptr{OthChr } , bpnt:: Ptr{OthChr } , len)
858+ function _memcmp (apnt:: Ptr{T } , bpnt:: Ptr{T } , len) where {T <: OthChr }
849859 fin = bytoff (apnt, len)
850860 while apnt < fin
851861 (c1 = get_codeunit (apnt)) == (c2 = get_codeunit (bpnt)) || return _cmp (c1, c2)
852- apnt += sizeof (OthChr )
853- bpnt += sizeof (OthChr )
862+ apnt += sizeof (T )
863+ bpnt += sizeof (T )
854864 end
855865 0
856866end
@@ -880,23 +890,17 @@ _memcmp(a::SubString{<:Str{<:Quad_CSEs}}, b::SubString{<:Str{Quad_CSEs}}, siz) =
880890
881891_memcpy (dst:: Ptr{UInt8} , src:: Ptr , siz) =
882892 ccall (:memcpy , Ptr{UInt8}, (Ptr{Cvoid}, Ptr{Cvoid}, UInt), dst, src, siz)
883- _memcpy (a:: Ptr{WidChr} , b:: Ptr{WidChr} , len) =
884- (HAS_WMEMCPY
885- ? ccall (:wmemcpy , Ptr{WidChr}, (Ptr{WidChr}, Ptr{WidChr}, UInt), a, b, len)
886- : ccall (:memcpy , Ptr{WidChr}, (Ptr{WidChr}, Ptr{WidChr}, UInt), a, b, bytoff (WidChr, len)))
887- _memcpy (a:: Ptr{OthChr} , b:: Ptr{OthChr} , len) =
888- ccall (:memcpy , Ptr{OthChr}, (Ptr{OthChr}, Ptr{OthChr}, UInt), a, b, bytoff (OthChr, len))
889-
890- @inline _memset (pnt:: Ptr{UInt8} , ch:: UInt8 , cnt) =
891- ccall (:memset , Ptr{UInt8}, (Ptr{UInt8}, Cint, Csize_t), pnt, ch, cnt)
892- @inline _memset (pnt:: Ptr{WidChr} , ch:: WidChr , cnt) =
893- ccall (:wmemset , Ptr{WidChr}, (Ptr{WidChr}, Cuint, Csize_t), pnt, ch, cnt)
894-
895- @inline function _memset (pnt:: Ptr{OthChr} , ch:: OthChr , cnt)
893+ _memcpy (a:: Ptr{T} , b:: Ptr{T} , len) where {T<: OthChr } =
894+ ccall (:memcpy , Ptr{T}, (Ptr{T}, Ptr{T}, UInt), a, b, bytoff (T, len))
895+
896+ @inline _memset (pnt:: Ptr{T} , ch:: T , cnt) where {T<: UInt8 } =
897+ ccall (:memset , Ptr{T}, (Ptr{T}, Cint, Csize_t), pnt, ch, cnt)
898+
899+ @inline function _memset (pnt:: Ptr{T} , ch:: T , cnt) where {T<: OthChr }
896900 fin = bytoff (pnt, cnt)
897901 while pnt < fin
898902 set_codeunit! (pnt, ch)
899- pnt += sizeof (OthChr )
903+ pnt += sizeof (T )
900904 end
901905end
902906
0 commit comments