Skip to content

Commit 96c851f

Browse files
authored
Add RepString from Base (#9)
* Add RepString from Base * Use isdefined * Mention RepString in the README [ci skip]
1 parent 4cb5e59 commit 96c851f

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
[![Julia 0.4 Status](http://pkg.julialang.org/badges/LegacyStrings_0.4.svg)](http://pkg.julialang.org/?pkg=LegacyStrings&ver=0.4)
77
[![Julia 0.5 Status](http://pkg.julialang.org/badges/LegacyStrings_0.5.svg)](http://pkg.julialang.org/?pkg=LegacyStrings&ver=0.5)
88

9-
The LegacyStrings package provides compatibility string types from Julia 0.4 (and earlier), which were removed in Julia 0.5, including:
9+
The LegacyStrings package provides compatibility string types from Julia 0.5 (and earlier), which were removed in subsequent versions, including:
1010

1111
- `ASCIIString`: a single-byte-per character string type that can only hold ASCII string data.
1212
- `UTF8String`: a string type with single byte code units (`UInt8`), encoding strings as UTF-8.
1313
- `UTF16String`: a string type with two-byte native-endian code units (`UInt16`), encoding strings as UTF-16.
1414
- `UTF32String`: a string type with four-byte native-endian code units (`UInt32`), encoding strings as UTF-32.
1515
- `ByteString`: a type alias for `Union{ASCIIString,UTF8String}`, i.e. strings that can be passed to C directly.
1616
- `WString`: an alias for `UTF16String` if `Cwchart_t` is two bytes (i.e. Windows) or `UTF32String` otherwise.
17+
- `RepString`: a string type for efficient handling of repeated strings.
1718

1819
LegacyStrings also defines and exports converter functions for these types, i.e.:
1920

src/LegacyStrings.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module LegacyStrings
77
export
88
ByteString,
99
ASCIIString,
10+
RepString,
1011
UTF8String,
1112
UTF16String,
1213
UTF32String,
@@ -81,4 +82,10 @@ import Base:
8182
else
8283
using Base: UTF_ERR_SHORT, checkstring
8384
end
85+
86+
if isdefined(Base, :RepString)
87+
using Base: RepString
88+
else
89+
include("rep.jl")
90+
end
8491
end # module

src/rep.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license
2+
3+
immutable RepString <: AbstractString
4+
string::AbstractString
5+
repeat::Integer
6+
end
7+
8+
function endof(s::RepString)
9+
e = endof(s.string)
10+
(next(s.string,e)[2]-1) * (s.repeat-1) + e
11+
end
12+
length(s::RepString) = length(s.string)*s.repeat
13+
sizeof(s::RepString) = sizeof(s.string)*s.repeat
14+
15+
function next(s::RepString, i::Int)
16+
if i < 1
17+
throw(BoundsError(s, i))
18+
end
19+
e = endof(s.string)
20+
sz = next(s.string,e)[2]-1
21+
22+
r, j = divrem(i-1, sz)
23+
j += 1
24+
25+
if r >= s.repeat || j > e
26+
throw(BoundsError(s, i))
27+
end
28+
29+
c, k = next(s.string, j)
30+
c, k-j+i
31+
end
32+
33+
convert(::Type{RepString}, s::AbstractString) = RepString(s,1)

test/runtests.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,31 @@ let str = ascii("this ")
518518
end
519519

520520
@test isvalid(Char['f','o','o','b','a','r'])
521+
522+
523+
## Repeat strings ##
524+
525+
# Base Julia issue #7764
526+
let
527+
rs = RepString("foo", 2)
528+
@test length(rs) == 6
529+
@test sizeof(rs) == 6
530+
@test isascii(rs)
531+
@test convert(RepString, "foobar") == "foobar"
532+
@test typeof(convert(RepString, "foobar")) == RepString
533+
534+
srep = RepString("Σβ",2)
535+
s="Σβ"
536+
ss=SubString(s,1,endof(s))
537+
538+
@test ss^2 == "ΣβΣβ"
539+
@test RepString(ss,2) == "ΣβΣβ"
540+
541+
@test endof(srep) == 7
542+
543+
@test next(srep, 3) == ('β',5)
544+
@test next(srep, 7) == ('β',9)
545+
546+
@test srep[7] == 'β'
547+
@test_throws BoundsError srep[8]
548+
end

0 commit comments

Comments
 (0)