Skip to content

Commit

Permalink
fix join/validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
acxz committed Jun 22, 2024
1 parent 720e682 commit cf29b5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ StringCases.convert("askBest30MWΠrice", my_pattern_case, StringCases.SNAKE_CASE
# Notice that the uppercase Greek letter Π denotes the start of a new token
# and is also lowercased as required by the snake case convention
StringCases.convert("askBest30MWΠrice", camel_case_acro_num, StringCases.SNAKE_CASE)
# Output: ask_best_30mw_πrice"
# Output: "ask_best_30mw_πrice"

# Say you don't know the string case of an input string, but you can extract the
# tokens of the input string, e.g. using Base.split or Base.eachmatch
Expand Down
31 changes: 25 additions & 6 deletions src/stringcases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ struct PatternStringCase{
end

# Case tokens based on string case
function _case!(tokens, sc::AbstractStringCase)
function case!(tokens, sc::AbstractStringCase)
# Case all token letters
tokens[begin:end] = sc.tokencase.(tokens)

Expand All @@ -244,7 +244,7 @@ end

# Case first char of token if not empty, otherwise passthrough
function _casefirst(token::AbstractString, case::UpperLowerTitleAnyCase)
if isempty("")
if isempty(token)
token
else
case(first(token)) * SubString(token, nextind(token, firstindex(token)))
Expand All @@ -260,12 +260,12 @@ function split(s::AbstractString, psc::PatternStringCase)
end

function join(tokens, dsc::DelimiterStringCase)
_case!(tokens, dsc)
case!(tokens, dsc)
return Base.join(tokens, dsc.dlm)
end

function join(tokens, psc::PatternStringCase)
_case!(tokens, psc)
case!(tokens, psc)
return Base.join(tokens)
end

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

## Validate the input string on the input string case
#if s != join(tokens, input_sc)
# throw(
# DomainError(
# (s, input_sc),
# """
# convert called with a string that disobeys input StringCase.
# Either create a new StringCase that describes the input string
# and call convert with the new input StringCase
# or split the string into tokens yourself
# and call join(tokens, output_string_case)
# """,
# ),
# )
#end

# Join tokens based on delimiter or pattern
return join(tokens, output_sc)
end

function isvalid(s::AbstractString, sc::AbstractStringCase)
# Validate converted string to the original string
return s == convert(s, sc, sc)
# Split string based on delimiter or pattern
tokens = split(s, sc)

# Validate the input string on the input string case
return s == join(tokens, sc)
end
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ using Test

@testset "StringCases.jl" begin
@testset "Validate" begin
@test StringCases.validate("StringCasePat", StringCases.PASCAL_CASE) == true
@test StringCases.validate("stringCasePat", StringCases.CAMEL_CASE) == false
@test StringCases.validate("String Case Dlm", StringCases.TITLE_CASE) == true
@test StringCases.validate("α_β_δ", StringCases.SNAKE_CASE) == true
@test StringCases.isvalid("StringCasePat", StringCases.PASCAL_CASE) == true
@test StringCases.isvalid("StringCasePat", StringCases.CAMEL_CASE) == false
@test StringCases.isvalid("String Case Dlm", StringCases.TITLE_CASE) == true
@test StringCases.isvalid("α_β_δ", StringCases.SNAKE_CASE) == true
end
@testset "Convert" begin
@test StringCases.convert(
Expand Down

0 comments on commit cf29b5a

Please sign in to comment.