Skip to content

Commit

Permalink
Adjust TrimmedString regex
Browse files Browse the repository at this point in the history
This should now match up with the strings that `String.trim` produces.
I had to change `.*` to `(?s:.*)` because the line separator character
isn't removed by `trim` yet doesn't match `.*` without the `s` flag, so
a string like `"\u2028"` can be the result of `trim` but didn't
previously match the regular expression.

Scalastyle and scalafmt had some competing opinions on how I should
format this long regex Witness expression.
  • Loading branch information
ceedubs committed Apr 26, 2018
1 parent 0b6ab12 commit 9a9a877
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ object string {

object NonEmptyString extends RefinedTypeOps[NonEmptyString, String]

/** A `String` that contains no leading or trailing whitespace. */
type TrimmedString = String Refined MatchesRegex[W.`"""^(?!\\s).*(?<!\\s)"""`.T]
// scalastyle:off no.whitespace.after.left.bracket
/**
* A `String` that contains no leading or trailing whitespace.
*
* Note that a line separator (`\u2028') is not considered whitespace for the
* purposes of trimming.
*/
type TrimmedString = String Refined MatchesRegex[
W.`"""^(?![\\x{0000}-\\x{0020}])(?s:.*)(?<![\\x{0000}-\\x{0020}])"""`.T]
// scalastyle:on no.whitespace.after.left.bracket

object TrimmedString extends RefinedTypeOps[TrimmedString, String] {

Expand All @@ -57,15 +65,6 @@ object string {
*
* scala> TrimmedString.trim(" \n a b c ")
* res0: TrimmedString = a b c
*
* Note that there are some strings can inhabit `TrimmedString` but will
* still be trimmed when passed into this method:
*
* scala> TrimmedString("\u0000a")
* res1: TrimmedString = \u000a
*
* scala> TrimmedString.trim("\u0000a")
* res2: TrimmedString = a
* }}}
*/
def trim(s: String): TrimmedString = Refined.unsafeApply(s.trim)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class StringTypesSpec extends Properties("StringTypes") {
(truncated.value ?= str.take(FString3.maxLength))
}

property("""TrimmedString.from(str)""") = forAll { (str: String) =>
TrimmedString.from(str).isRight ?= (TrimmedString.trim(str).value == str)
}

property("""TrimmedString.trim(str)""") = forAll { (str: String) =>
val trimmed = TrimmedString.trim(str)
TrimmedString.from(trimmed.value) ?= Right(trimmed)
Expand Down

0 comments on commit 9a9a877

Please sign in to comment.