From 6484f4cebfc38cc6dc6934dd6eb4d8b544c27e59 Mon Sep 17 00:00:00 2001 From: Benjamin Deonovic Date: Sat, 31 Dec 2016 15:22:10 -0600 Subject: [PATCH] Add `one` for AbstractString (#19548) Add `one` for AbstractString --- base/strings/basic.jl | 2 ++ test/strings/basic.jl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/base/strings/basic.jl b/base/strings/basic.jl index daa3c8c057a52..876743eead9ec 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -106,6 +106,8 @@ julia> "Hello " * "world" """ (*)(s1::AbstractString, ss::AbstractString...) = string(s1, ss...) +one{T<:AbstractString}(::Union{T,Type{T}}) = convert(T, "") + length(s::DirectIndexString) = endof(s) """ diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 60636e1cdd3ff..d7556f339826e 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -416,6 +416,10 @@ foobaz(ch) = reinterpret(Char, typemax(UInt32)) @test ["b","c"].*"a" == ["ba","ca"] @test ["a","b"].*["c" "d"] == ["ac" "ad"; "bc" "bd"] +@test one(String) == "" +@test prod(["*" for i in 1:3]) == "***" +@test prod(["*" for i in 1:0]) == "" + # Make sure NULL pointers are handled consistently by String @test_throws ArgumentError unsafe_string(Ptr{UInt8}(0)) @test_throws ArgumentError unsafe_string(Ptr{UInt8}(0), 10)