| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4abc45a commit c82c084
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1922,6 +1922,13 @@ function formatWithOptionsInternal(inspectOptions, ...args) { | |||
| 1922 | 1922 | return str; | |
| 1923 | 1923 | } | |
| 1924 | 1924 | ||
| 1925 | + function prepareStringForGetStringWidth(str, removeControlChars) { | ||
| 1926 | + str = str.normalize('NFC'); | ||
| 1927 | + if (removeControlChars) | ||
| 1928 | + str = stripVTControlCharacters(str); | ||
| 1929 | + return str; | ||
| 1930 | + } | ||
| 1931 | + | ||
| 1925 | 1932 | if (internalBinding('config').hasIntl) { | |
| 1926 | 1933 | const icu = internalBinding('icu'); | |
| 1927 | 1934 | // icu.getStringWidth(string, ambiguousAsFullWidth, expandEmojiSequence) | |
@@ -1931,8 +1938,8 @@ if (internalBinding('config').hasIntl) { | |||
| 1931 | 1938 | // the receiving end supports. | |
| 1932 | 1939 | getStringWidth = function getStringWidth(str, removeControlChars = true) { | |
| 1933 | 1940 | let width = 0; | |
| 1934 | - if (removeControlChars) | ||
| 1935 | - str = stripVTControlCharacters(str); | ||
| 1941 | + | ||
| 1942 | + str = prepareStringForGetStringWidth(str, removeControlChars); | ||
| 1936 | 1943 | for (let i = 0; i < str.length; i++) { | |
| 1937 | 1944 | // Try to avoid calling into C++ by first handling the ASCII portion of | |
| 1938 | 1945 | // the string. If it is fully ASCII, we skip the C++ part. | |
@@ -1952,9 +1959,7 @@ if (internalBinding('config').hasIntl) { | |||
| 1952 | 1959 | getStringWidth = function getStringWidth(str, removeControlChars = true) { | |
| 1953 | 1960 | let width = 0; | |
| 1954 | 1961 | ||
| 1955 | - if (removeControlChars) | ||
| 1956 | - str = stripVTControlCharacters(str); | ||
| 1957 | - | ||
| 1962 | + str = prepareStringForGetStringWidth(str, removeControlChars); | ||
| 1958 | 1963 | for (const char of str) { | |
| 1959 | 1964 | const code = char.codePointAt(0); | |
| 1960 | 1965 | if (isFullWidthCodePoint(code)) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -87,3 +87,12 @@ for (let i = 0; i < 256; i++) { | |||
| 87 | 87 | assert.strictEqual(getStringWidth(char), 1); | |
| 88 | 88 | } | |
| 89 | 89 | } | |
| 90 | + | ||
| 91 | + { | ||
| 92 | + const a = '한글'.normalize('NFD'); // 한글 | ||
| 93 | + const b = '한글'.normalize('NFC'); // 한글 | ||
| 94 | + assert.strictEqual(a.length, 6); | ||
| 95 | + assert.strictEqual(b.length, 2); | ||
| 96 | + assert.strictEqual(getStringWidth(a), 4); | ||
| 97 | + assert.strictEqual(getStringWidth(b), 4); | ||
| 98 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments