Skip to content

Commit 8d59ca0

Browse files
committed
Add back RopeString type for now
1 parent f0ebe22 commit 8d59ca0

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

base/stringtypes.jl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

3-
# SubString, RevString, and RepString types
3+
# SubString, RevString, RepString, and RopeString types
44

55
## substrings reference original strings ##
66

@@ -176,3 +176,49 @@ function repeat(s::ByteString, r::Integer)
176176
end
177177

178178
(^)(s::AbstractString, r::Integer) = repeat(s,r)
179+
180+
## ropes for efficient concatenation, etc. ##
181+
182+
immutable RopeString <: AbstractString
183+
head::AbstractString
184+
tail::AbstractString
185+
depth::Int32
186+
endof::Int
187+
188+
RopeString(h::RopeString, t::RopeString) =
189+
strdepth(h.tail) + strdepth(t) < strdepth(h.head) ?
190+
RopeString(h.head, RopeString(h.tail, t)) :
191+
new(h, t, max(h.depth,t.depth)+1, endof(h)+endof(t))
192+
193+
RopeString(h::RopeString, t::AbstractString) =
194+
strdepth(h.tail) < strdepth(h.head) ?
195+
RopeString(h.head, RopeString(h.tail, t)) :
196+
new(h, t, h.depth+1, endof(h)+endof(t))
197+
198+
RopeString(h::AbstractString, t::RopeString) =
199+
strdepth(t.head) < strdepth(t.tail) ?
200+
RopeString(RopeString(h, t.head), t.tail) :
201+
new(h, t, t.depth+1, endof(h)+endof(t))
202+
203+
RopeString(h::AbstractString, t::AbstractString) =
204+
new(h, t, 1, endof(h)+endof(t))
205+
end
206+
RopeString(s::AbstractString) = RopeString(s,"")
207+
208+
strdepth(s::AbstractString) = 0
209+
strdepth(s::RopeString) = s.depth
210+
211+
function next(s::RopeString, i::Int)
212+
eh = endof(s.head)
213+
if i <= eh
214+
return next(s.head, i)
215+
else
216+
c, j = next(s.tail, i-eh)
217+
return c, j+eh
218+
end
219+
end
220+
221+
endof(s::RopeString) = s.endof
222+
length(s::RopeString) = length(s.head) + length(s.tail)
223+
write(io::IO, s::RopeString) = (write(io, s.head); write(io, s.tail))
224+
sizeof(s::RopeString) = sizeof(s.head) + sizeof(s.tail)

test/stringtypes.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,7 @@ let
190190
@test srep[7] == 'β'
191191
@test_throws BoundsError srep[8]
192192
end
193+
194+
## Rope strings ##
195+
196+
@test sizeof(RopeString("abc","def")) == 6

0 commit comments

Comments
 (0)