Skip to content

Commit afca4d9

Browse files
Efficient methods for memcat(a), memcat(s); strcpy.
For the nullary, use more idiomatic Array(Int8,0).
1 parent 9f62648 commit afca4d9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

j/string.j

+15-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,15 @@ end
793793

794794
# concatenate byte arrays into a single array
795795

796-
memcat() = Array{Uint8,1}((0,))
796+
memcat() = Array(Int8,0)
797+
798+
function memcat(a::Array{Uint8,1})
799+
b = Array(Uint8, length(a))
800+
ccall(dlsym(libc, :memcpy), Ptr{Uint8},
801+
(Ptr{Uint8}, Ptr{Uint8}, Ulong),
802+
pointer(b), pointer(a), ulong(length(a)))
803+
return b
804+
end
797805

798806
function memcat(arrays::Array{Uint8,1}...)
799807
n = 0
@@ -814,6 +822,8 @@ end
814822

815823
# concatenate the data fields of byte strings
816824

825+
memcat(s::ByteString) = memcat(s.data)
826+
817827
function memcat(strs::ByteString...)
818828
n = 0
819829
for s = strs
@@ -830,3 +840,7 @@ function memcat(strs::ByteString...)
830840
end
831841
data
832842
end
843+
844+
# copying a byte string (generally not needed)
845+
846+
strcpy{T<:ByteString}(s::T) = T(memcat(s))

0 commit comments

Comments
 (0)