trevorld / bittermelon
Showing 2 of 8 files from the diff.
Newly tracked file
R/utils-unicode.R created.
Other files ignored by Codecov

@@ -0,0 +1,25 @@
Loading
1 +
#' Other Unicode utilities
2 +
#'
3 +
#' `ucp2label()` returns Unicode code point \dQuote{labels} as a character vector.
4 +
#' `is_combining_character()` returns `TRUE` if the character is a \dQuote{combining} character.
5 +
#'
6 +
#' @param x A character vector of Unicode code points.
7 +
#' @return `ucp2name()` returns a character vector of Unicode labels.
8 +
#' @examples
9 +
#'   # Get the Unicode Code Point "label" for "R"
10 +
#'   ucp2label(str2ucp("R"))
11 +
#'
12 +
#'   is_combining_character(str2ucp("a"))
13 +
#'   is_combining_character("U+0300") # COMBINING GRAVE ACCENT
14 +
#' @seealso [hex2ucp()], [int2ucp()], [name2ucp()], and [str2ucp()] all return Unicode code points.
15 +
#' @rdname unicode_utilities
16 +
#' @export
17 +
ucp2label <- function(x) {
18 +
    Unicode::u_char_label(x)
19 +
}
20 +
21 +
#' @rdname unicode_utilities
22 +
#' @export
23 +
is_combining_character <- function(x) {
24 +
    Unicode::u_char_property(x, "Canonical_Combining_Class") > 0L
25 +
}

@@ -2,7 +2,6 @@
Loading
2 2
#'
3 3
#' `hex2ucp()`, `int2ucp()`, `name2ucp()`, and `str2ucp()` return
4 4
#' Unicode code points as character vectors.
5 -
#' `ucp2label` returns Unicode code point \dQuote{labels} as a character vector.
6 5
#'
7 6
#' `hex2ucp(x)` is a wrapper for `as.character(Unicode::as.u_char(x))`.
8 7
#' `int2ucp` is a wrapper for `as.character(Unicode::as.u_char(as.integer(x)))`.
@@ -13,12 +12,10 @@
Loading
13 12
#' by these functions and not `Unicode::u_char` objects.
14 13
#' @param x R objects coercible to the respective Unicode character data types.
15 14
#'          See [Unicode::as.u_char()] for `hex2ucp()` and `int2ucp()`,
16 -
#'          [base::utf8ToInt()] for `str2ucp()`,
17 -
#'          [Unicode::u_char_from_name()] for `name2ucp()`,
18 -
#'          and [Unicode::u_char_label()] for `ucp2name()`.
15 +
#'          [base::utf8ToInt()] for `str2ucp()`, and
16 +
#'          [Unicode::u_char_from_name()] for `name2ucp()`.
19 17
#' @inheritParams Unicode::u_char_from_name
20 -
#' @return A character vector of Unicode code points except for
21 -
#'         `ucp2name` which returns a character vector of Unicode labels.
18 +
#' @return A character vector of Unicode code points.
22 19
#' @examples
23 20
#'   # These are all different ways to get the same 'R' code point
24 21
#'   hex2ucp("52")
@@ -29,16 +26,15 @@
Loading
29 26
#'   int2ucp(82) # 82 == as.hexmode("52")
30 27
#'   int2ucp("82") # 82 == as.hexmode("52")
31 28
#'   int2ucp(utf8ToInt("R"))
29 +
#'   ucp2label("U+0052")
32 30
#'   name2ucp("LATIN CAPITAL LETTER R")
33 31
#'   str2ucp("R")
34 32
#'
35 -
#'   # Get the Unicode Code Point "label"
36 -
#'   ucp2label("U+0052")
37 -
#'
38 33
#'   # Potential gotcha as as.hexmode("52") == as.integer("82") == 52L
39 34
#'   all.equal(hex2ucp(52L), int2ucp(52L)) # TRUE
40 35
#'   all.equal(hex2ucp("52"), int2ucp("82")) # TRUE
41 36
#'   all.equal(hex2ucp("82"), int2ucp("82")) # FALSE
37 +
#' @seealso [ucp2label()] and [is_combining_character()].
42 38
#' @rdname unicode_code_points
43 39
#' @export
44 40
hex2ucp <- function(x) {
@@ -57,17 +53,11 @@
Loading
57 53
#' @rdname unicode_code_points
58 54
#' @export
59 55
str2ucp <- function(x) {
60 -
    int2ucp(utf8ToInt(x))
61 -
}
62 -
63 -
#' @rdname unicode_code_points
64 -
#' @export
65 -
name2ucp <- function(x, type = c("exact", "grep")) {
66 -
    as.character(Unicode::u_char_from_name(x, type = type))
56 +
    sapply(x, function(s) int2ucp(utf8ToInt(s)), USE.NAMES = FALSE)
67 57
}
68 58
69 59
#' @rdname unicode_code_points
70 60
#' @export
71 -
ucp2label <- function(x) {
72 -
    Unicode::u_char_label(x)
61 +
name2ucp <- function(x, type = c("exact", "grep"), ...) {
62 +
    as.character(Unicode::u_char_from_name(toupper(x), type = type, ...))
73 63
}
Files Coverage
R 91.36%
Project Totals (28 files) 91.36%
Untitled

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading