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 Char#printable? #11429

Merged

Conversation

straight-shoota
Copy link
Member

This method defines whether a character is printable.

Preparation for #11406

src/char.cr Outdated Show resolved Hide resolved
src/char.cr Outdated Show resolved Hide resolved
Co-authored-by: Jason Frey <[email protected]>
src/char.cr Show resolved Hide resolved
' '.printable?.should be_true
'a'.printable?.should be_true
'酒'.printable?.should be_true
'\n'.printable?.should be_false
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd think that the newline character is included. It's certainly visible, and printable - same as ASCII whitespace.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking similar for tab. I guess it depends what the purpose of this method is apart from what's needed in #11406

Copy link
Member Author

Choose a reason for hiding this comment

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

These characters are typically considered non-printable. For example, in other programming languages:

# Python
"\n".isprintable() # => false
"\t".isprintable() # => false
" ".isprintable()  # => true
# Go
unicode.IsPrint('\n') // => false
unicode.IsPrint('\t') // => false
unicode.IsPrint(' ')  // => true

I think the reasoning is that white space has no visible glyph. It's just space. Hence you can't even distinguish between different white space characters. ASCII white space is the only exception which improves usability and is totally fine because having only a single whitespace character being printable makes it unambiguous.

@HertzDevil
Copy link
Contributor

This definition coincides with Go's (own) definiton, by the way: https://pkg.go.dev/unicode#IsPrint

@straight-shoota
Copy link
Member Author

@HertzDevil Yes, that and Python's str.isprintable have served as inspiration.

@straight-shoota straight-shoota added this to the 1.3.0 milestone Nov 12, 2021
@straight-shoota straight-shoota merged commit 0fbf266 into crystal-lang:master Nov 14, 2021
@straight-shoota straight-shoota deleted the feature/char-printable branch November 14, 2021 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants