Skip to content

Commit

Permalink
more verbose multi-line display(c) for Char (#19847)
Browse files Browse the repository at this point in the history
* more verbose multi-line display(c) for Char
  • Loading branch information
stevengj committed Jan 5, 2017
1 parent b53cd8b commit f25d5a7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
7 changes: 7 additions & 0 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ function show(io::IO, c::Char)
end
return
end

function show(io::IO, ::MIME"text/plain", c::Char)
show(io, c)
u = UInt32(c)
print(io, ": ", isascii(c) ? "ASCII/" : "", "Unicode U+", hex(u, u > 0xffff ? 6 : 4))
print(io, " (category ", UTF8proc.category_abbrev(c), ": ", UTF8proc.category_string(c), ")")
end
9 changes: 4 additions & 5 deletions base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ export Display, display, pushdisplay, popdisplay, displayable, redisplay,
# that Julia's dispatch and overloading mechanisms can be used to
# dispatch show and to add conversions for new types.

immutable MIME{mime} end
# defined in sysimg.jl for bootstrapping:
# immutable MIME{mime} end
# macro MIME_str(s)
import Base: MIME, @MIME_str

import Base: show, print, string, convert
MIME(s) = MIME{Symbol(s)}()
show{mime}(io::IO, ::MIME{mime}) = print(io, "MIME type ", string(mime))
print{mime}(io::IO, ::MIME{mime}) = print(io, mime)

macro MIME_str(s)
:(MIME{$(Expr(:quote, Symbol(s)))})
end

###########################################################################
# For any type T one can define show(io, ::MIME"type", x::T) = ...
# in order to provide a way to export T as a given mime type.
Expand Down
44 changes: 40 additions & 4 deletions base/strings/utf8proc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module UTF8proc

import Base: show, ==, hash, string, Symbol, isless, length, eltype, start, next, done, convert, isvalid, lowercase, uppercase, titlecase

export isgraphemebreak
export isgraphemebreak, category_code, category_abbrev, category_string

# also exported by Base:
export normalize_string, graphemes, is_assigned_char, charwidth, isvalid,
Expand Down Expand Up @@ -51,6 +51,40 @@ const UTF8PROC_CATEGORY_CF = 27
const UTF8PROC_CATEGORY_CS = 28
const UTF8PROC_CATEGORY_CO = 29

# strings corresponding to the category constants
const category_strings = [
"Other, not assigned",
"Letter, uppercase",
"Letter, lowercase",
"Letter, titlecase",
"Letter, modifier",
"Letter, other",
"Mark, nonspacing",
"Mark, spacing combining",
"Mark, enclosing",
"Number, decimal digit",
"Number, letter",
"Number, other",
"Punctuation, connector",
"Punctuation, dash",
"Punctuation, open",
"Punctuation, close",
"Punctuation, initial quote",
"Punctuation, final quote",
"Punctuation, other",
"Symbol, math",
"Symbol, currency",
"Symbol, modifier",
"Symbol, other",
"Separator, space",
"Separator, line",
"Separator, paragraph",
"Other, control",
"Other, format",
"Other, surrogate",
"Other, private use"
]

const UTF8PROC_STABLE = (1<<1)
const UTF8PROC_COMPAT = (1<<2)
const UTF8PROC_COMPOSE = (1<<3)
Expand Down Expand Up @@ -164,9 +198,11 @@ titlecase(c::Char) = isascii(c) ? ('a' <= c <= 'z' ? c - 0x20 : c) : Char(ccall(
############################################################################

# returns UTF8PROC_CATEGORY code in 0:30 giving Unicode category
function category_code(c)
return ccall(:utf8proc_category, Cint, (UInt32,), c)
end
category_code(c) = ccall(:utf8proc_category, Cint, (UInt32,), c)

# more human-readable representations of the category code
category_abbrev(c) = unsafe_string(ccall(:utf8proc_category_string, Cstring, (UInt32,), c))
category_string(c) = category_strings[category_code(c)+1]

"""
is_assigned_char(c) -> Bool
Expand Down
6 changes: 6 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ include("io.jl")
include("iostream.jl")
include("iobuffer.jl")

# define MIME"foo/bar" early so that we can overload 3-arg show
immutable MIME{mime} end
macro MIME_str(s)
:(MIME{$(Expr(:quote, Symbol(s)))})
end

# strings & printing
include("char.jl")
include("intfuncs.jl")
Expand Down
3 changes: 3 additions & 0 deletions test/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,6 @@ let
end

@test !isequal('x', 120)

@test sprint(show, "text/plain", '$') == "'\$': ASCII/Unicode U+0024 (category Sc: Symbol, currency)"
@test repr('$') == "'\$'"

0 comments on commit f25d5a7

Please sign in to comment.