From bec118635cfc8566ac3dedee800050f21ed6dac9 Mon Sep 17 00:00:00 2001 From: Wynand Badenhorst Date: Tue, 3 Aug 2021 15:32:27 -0500 Subject: [PATCH] Add support for empty strings --- Project.toml | 2 +- src/base.jl | 2 +- test/runtests.jl | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 09bbf6a..b8e0f16 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ShortStrings" uuid = "63221d1c-8677-4ff0-9126-0ff0817b4975" authors = ["Dai ZJ ", "ScottPJones ", "Lyndon White "] -version = "0.3.10" +version = "0.3.11" [deps] BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1" diff --git a/src/base.jl b/src/base.jl index 64adf44..ee98fd3 100644 --- a/src/base.jl +++ b/src/base.jl @@ -320,7 +320,7 @@ If the keyword argument `types` is passed with a list (a tuple or Vector) of Uns types, in order of their size, then one of those types will be used. """ ShortString(str::Union{String,SubString{String}}, maxlen = sizeof(str); types=def_types) = - get_type(maxlen, types=types)(str) + get_type(max(maxlen,1), types=types)(str) """ Create a ShortString, using the smallest ShortString that can fit the string, diff --git a/test/runtests.jl b/test/runtests.jl index 2d64c44..219a05a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -72,9 +72,13 @@ ss = ShortString15(s) @test ss15"Short String!!!" === ShortString15("Short String!!!") @test ss7"ShrtStr" === ShortString7("ShrtStr") @test ss3"ss3" === ShortString3("ss3") +@test ss"" === ShortString("") @testset "equality of different sized ShortStrings" begin + @test ShortString15("") == ShortString3("") + @test ShortString3("") == ShortString15("") + @test ShortString15("ab") == ShortString3("ab") @test ShortString3("ab") == ShortString15("ab") @@ -172,3 +176,11 @@ end @test split(ShortString15("abc XYZ x")) == ["abc", "XYZ", "x"] @test split(ShortString15("abc XYZ x")) isa Vector{SubString{ShortString15}} end + +@testset "test all default valid string sizes" begin + size_limit = 255 + svec_good = [randstring(i) for i=0:size_limit] + @test typeof(ShortString.(svec_good)) == Vector{ShortString} + s_bad = randstring(size_limit + 1) + @test_throws ArgumentError ShortString(s_bad) +end