Skip to content

Commit

Permalink
Add Char#printable? (#11429)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Frey <[email protected]>
  • Loading branch information
straight-shoota and Fryguy authored Nov 14, 2021
1 parent 170d737 commit 0fbf266
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spec/std/char_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ describe "Char" do
'酒'.ascii?.should be_false
end

it "#printable?" do
' '.printable?.should be_true
'a'.printable?.should be_true
'酒'.printable?.should be_true
'\n'.printable?.should be_false
'\e'.printable?.should be_false
'\uF8FF'.printable?.should be_false
end

describe "clone" do
it { 'a'.clone.should eq('a') }
end
Expand Down
14 changes: 13 additions & 1 deletion src/char.cr
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,23 @@ struct Char
ascii? ? ascii_control? : Unicode.control?(self)
end

# Returns `true` if this is char is a mark character according to unicode.
# Returns `true` if this char is a mark character according to unicode.
def mark? : Bool
Unicode.mark?(self)
end

# Returns `true` if this char is a printable character.
#
# There is no universal definition of printable characters in Unicode.
# For the purpose of this method, all characters with a visible glyph and the
# ASCII whitespace (` `) are considered printable.
#
# This means characters which are `control?` or `whitespace?` (except for ` `)
# are non-printable.
def printable?
!control? && (!whitespace? || self == ' ')
end

# Returns this char as a string that contains a char literal.
#
# ```
Expand Down

0 comments on commit 0fbf266

Please sign in to comment.