GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cli/R/unicode.R at main · cderv/cli

Expand file tree

/

unicode.R

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (49 loc) · 1.96 KB
/

unicode.R

Copy path

File metadata and controls

53 lines (49 loc) · 1.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#' Working around the bad Unicode character widths
#'
#' R 3.6.2 and also the coming 3.6.3 and 4.0.0 versions use the Unicode 8
#' standard to calculate the display width of Unicode characters.
#' Unfortunately the widths of most emojis are incorrect in this standard,
#' and width 1 is reported instead of the correct 2 value.
#'
#' cli implements a workaround for this. The package contains a table that
#' contains all Unicode ranges that have wide characters (display width 2).
#'
#' On first use of one of the workaround wrappers (in `ansi_nchar()`, etc.)
#' we check what the current version of R thinks about the width of these
#' characters, and then create a regex that matches the ones that R
#' is wrong about (`re_bad_char_width`).
#'
#' Then we use this regex to duplicate all of the problematic characters
#' in the input string to the wrapper function, before calling the real
#' string manupulation function (char, strwrap) etc. At end we undo the
#' duplication before we return the result.
#'
#' This workaround is fine for `nchar()` and `strwrap()`, and consequently
#' `ansi_align()` and `ansi_strtrim()` as well.
#'
#' The rest of the `ansi_*()` functions work on characters, and do not
#' deal with character width.
#'
#' @keywords internal
#' @name unicode-width-workaround
NULL
setup_unicode_width_fix <- function() {
bad <- base::nchar(wide_chars$test, type = "width") == 1
re <- paste0(wide_chars$regex[bad], collapse = "")
clienv$re_bad_char_width <- paste0("([", re, "])")
clienv$re_bad_char_width_fix <- paste0("([", re, "])\\1")
}
unicode_pre <- function(x) {
if (is.null(clienv$re_bad_char_width)) setup_unicode_width_fix()
if (clienv$re_bad_char_width != "([])") {
x <- gsub(clienv$re_bad_char_width, "\\1\\1", x, perl = TRUE)
}
x
}
unicode_post <- function(x) {
if (is.null(clienv$re_bad_char_width)) setup_unicode_width_fix()
if (clienv$re_bad_char_width != "([])") {
x <- gsub(clienv$re_bad_char_width_fix, "\\1", x, perl = TRUE)
}
x
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.