Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ascii/latin1/unicode character generators #96

Merged
merged 2 commits into from
May 16, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Hedgehog/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ module Gen =
let char (lo : char) (hi : char) : Gen<char> =
integral <| Range.constant (int lo) (int hi) |> map char

/// Generates a random character.
let charBounded : Gen<char> =
/// Generates a Unicode character, including invalid standalone surrogates:
/// '\000'..'\65535'
let unicodeAll : Gen<char> =
let lo = System.Char.MinValue
let hi = System.Char.MaxValue
char lo hi
Expand All @@ -338,6 +339,19 @@ module Gen =
let upper : Gen<char> =
char 'A' 'Z'

/// Generates an ASCII character: '\000'..'\127'
let ascii : Gen<char> =
char '\000' '\127'

/// Generates a Latin-1 character: '\000'..'\255'
let latin1 : Gen<char> =
char '\000' '\255'

/// Generates a Unicode character, excluding invalid standalone surrogates:
/// '\000'..'\65535' (excluding '\55296'..'\57343')
let unicode : Gen<char> =
filter (not << System.Char.IsSurrogate) unicodeAll

// Generates a random alpha character.
let alpha : Gen<char> =
choice [lower; upper]
Expand All @@ -354,7 +368,7 @@ module Gen =

/// Generates a random string.
let string : Gen<string> =
choice [alphaNum; charBounded] |> string'
choice [alphaNum; unicodeAll] |> string'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be just unicode given the surrogates won't be a legal string unless they're combined correctly? (sorry I don't know that much about unicode surrogates, but I get the impression this wouldn't be a valid string as such)

Copy link
Member Author

@moodmosaic moodmosaic May 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... True, unicode is a better default, I'll change it.

And, I think, we need something similar in the Haskell version? Right now it uses min/max bounds of char, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is a haskell version, the haskell Gen.string takes a Gen<char> as an argument

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done now in 4979fb5.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I wonder if we should just rename string' -> string and delete this, let people make their own choices for characters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was exactly what I was thinking right now... We're going to have Gen.fs in line with the Haskell one anyway, so good idea. I'll undo 4979fb5 also.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I just pushed f39c328.


//
// Combinators - Primitives
Expand Down