Skip to content

Commit cf29b5a

Browse files
committed
fix join/validation code
1 parent 720e682 commit cf29b5a

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ StringCases.convert("askBest30MWΠrice", my_pattern_case, StringCases.SNAKE_CASE
125125
# Notice that the uppercase Greek letter Π denotes the start of a new token
126126
# and is also lowercased as required by the snake case convention
127127
StringCases.convert("askBest30MWΠrice", camel_case_acro_num, StringCases.SNAKE_CASE)
128-
# Output: ask_best_30mw_πrice"
128+
# Output: "ask_best_30mw_πrice"
129129

130130
# Say you don't know the string case of an input string, but you can extract the
131131
# tokens of the input string, e.g. using Base.split or Base.eachmatch

src/stringcases.jl

+25-6
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ struct PatternStringCase{
231231
end
232232

233233
# Case tokens based on string case
234-
function _case!(tokens, sc::AbstractStringCase)
234+
function case!(tokens, sc::AbstractStringCase)
235235
# Case all token letters
236236
tokens[begin:end] = sc.tokencase.(tokens)
237237

@@ -244,7 +244,7 @@ end
244244

245245
# Case first char of token if not empty, otherwise passthrough
246246
function _casefirst(token::AbstractString, case::UpperLowerTitleAnyCase)
247-
if isempty("")
247+
if isempty(token)
248248
token
249249
else
250250
case(first(token)) * SubString(token, nextind(token, firstindex(token)))
@@ -260,12 +260,12 @@ function split(s::AbstractString, psc::PatternStringCase)
260260
end
261261

262262
function join(tokens, dsc::DelimiterStringCase)
263-
_case!(tokens, dsc)
263+
case!(tokens, dsc)
264264
return Base.join(tokens, dsc.dlm)
265265
end
266266

267267
function join(tokens, psc::PatternStringCase)
268-
_case!(tokens, psc)
268+
case!(tokens, psc)
269269
return Base.join(tokens)
270270
end
271271

@@ -277,11 +277,30 @@ function convert(
277277
# Split string based on delimiter or pattern
278278
tokens = split(s, input_sc)
279279

280+
## Validate the input string on the input string case
281+
#if s != join(tokens, input_sc)
282+
# throw(
283+
# DomainError(
284+
# (s, input_sc),
285+
# """
286+
# convert called with a string that disobeys input StringCase.
287+
# Either create a new StringCase that describes the input string
288+
# and call convert with the new input StringCase
289+
# or split the string into tokens yourself
290+
# and call join(tokens, output_string_case)
291+
# """,
292+
# ),
293+
# )
294+
#end
295+
280296
# Join tokens based on delimiter or pattern
281297
return join(tokens, output_sc)
282298
end
283299

284300
function isvalid(s::AbstractString, sc::AbstractStringCase)
285-
# Validate converted string to the original string
286-
return s == convert(s, sc, sc)
301+
# Split string based on delimiter or pattern
302+
tokens = split(s, sc)
303+
304+
# Validate the input string on the input string case
305+
return s == join(tokens, sc)
287306
end

test/runtests.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ using Test
33

44
@testset "StringCases.jl" begin
55
@testset "Validate" begin
6-
@test StringCases.validate("StringCasePat", StringCases.PASCAL_CASE) == true
7-
@test StringCases.validate("stringCasePat", StringCases.CAMEL_CASE) == false
8-
@test StringCases.validate("String Case Dlm", StringCases.TITLE_CASE) == true
9-
@test StringCases.validate("α_β_δ", StringCases.SNAKE_CASE) == true
6+
@test StringCases.isvalid("StringCasePat", StringCases.PASCAL_CASE) == true
7+
@test StringCases.isvalid("StringCasePat", StringCases.CAMEL_CASE) == false
8+
@test StringCases.isvalid("String Case Dlm", StringCases.TITLE_CASE) == true
9+
@test StringCases.isvalid("α_β_δ", StringCases.SNAKE_CASE) == true
1010
end
1111
@testset "Convert" begin
1212
@test StringCases.convert(

0 commit comments

Comments
 (0)