This commit is contained in:
flywind 2021-07-13 15:22:33 +08:00 • committed by GitHub
commit 15fdcd0c4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,29 +12,31 @@
## ##
## The following example shows how to change character encodings. ## The following example shows how to change character encodings.
runnableExamples: runnableExamples:
let when defined(windows):
orig = "öäüß" let
# convert `orig` from "UTF-8" to "CP1252" orig = "öäüß"
cp1252 = convert(orig, "CP1252", "UTF-8") # convert `orig` from "UTF-8" to "CP1252"
# convert `cp1252` from "CP1252" to "ibm850" cp1252 = convert(orig, "CP1252", "UTF-8")
ibm850 = convert(cp1252, "ibm850", "CP1252") # convert `cp1252` from "CP1252" to "ibm850"
current = getCurrentEncoding() ibm850 = convert(cp1252, "ibm850", "CP1252")
assert orig == "\195\182\195\164\195\188\195\159" current = getCurrentEncoding()
assert ibm850 == "\148\132\129\225" assert orig == "\195\182\195\164\195\188\195\159"
assert convert(ibm850, current, "ibm850") == orig assert ibm850 == "\148\132\129\225"
assert convert(ibm850, current, "ibm850") == orig
## The example below uses a reuseable `EncodingConverter` object which is ## The example below uses a reuseable `EncodingConverter` object which is
## created by `open` with `destEncoding` and `srcEncoding` specified. You can use ## created by `open` with `destEncoding` and `srcEncoding` specified. You can use
## `convert` on this object multiple times. ## `convert` on this object multiple times.
runnableExamples: runnableExamples:
var fromGB2312 = open("utf-8", "gb2312") when defined(windows):
let first = "\203\173\197\194\163\191\210\187" & var fromGB2312 = open("utf-8", "gb2312")
"\203\242\209\204\211\234\200\206\198\189\201\250" let first = "\203\173\197\194\163\191\210\187" &
assert fromGB2312.convert(first) == "谁怕?一蓑烟雨任平生" "\203\242\209\204\211\234\200\206\198\189\201\250"
assert fromGB2312.convert(first) == "谁怕?一蓑烟雨任平生"
let second = "\211\208\176\215\205\183\200\231" & let second = "\211\208\176\215\205\183\200\231" &
"\208\194\163\172\199\227\184\199\200\231\185\202" "\208\194\163\172\199\227\184\199\200\231\185\202"
assert fromGB2312.convert(second) == "有白头如新,倾盖如故" assert fromGB2312.convert(second) == "有白头如新,倾盖如故"
import os import os