蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

文本实例:按 UTF-8 边界读取采样标记

JULIAJulia 1.10.13 官方便携版,Windows x64,本机隔离目录text-strings.jl
text-strings.jl86 行

include(joinpath(@__DIR__, "observations.jl"))

letter = 'q'
label = "Harbor"
quoted = """Site "Harbor" recorded"""
probe("text_literals", "single, double and triple quotes",
      (typeof(letter),typeof(label),String <: AbstractString,Char <: AbstractChar,quoted),
      (Char,String,true,true,"Site \"Harbor\" recorded"))
probe("text_char_code", (letter,'海','🌊'), (Int(letter),Int('海'),Int('🌊'),sizeof(Char)),
      (113,0x6d77,0x1f30a,4))
probe("text_code_char", (97,0x6d77), (Char(97),Char(0x6d77)), ('a','海'))
probe("text_char_arithmetic", ('A','Z','q'), ('A'<'a','A'<='Q'<='Z','q'-'a','A'+2),
      (true,true,16,'C'))
continued = "Harbor \
            station"
probe("text_continuation", "backslash before a physical newline", continued, "Harbor station")
probe("text_ascii_index", label, (label[begin],label[1],label[2],label[end],label[end-1]),
      ('H','H','a','r','o'))
probe("text_ascii_slice", label, (label[begin:end],label[begin:end-1],label[2:4]),
      ("Harbor","Harbo","arb"))
probe("text_char_slice", label, (typeof(label[2]),typeof(label[2:2]),label[2:2]),
      (Char,String,"a"))
slice = SubString(label,1,3)
probe("text_substring", (label,1,3), (slice,typeof(slice)), ("Har",SubString{String}))

s = "A海🌊e\u0301"
probe("text_utf8", s, (s[1],s[2],s[5],outcome(() -> s[3]),collect(eachindex(s)),
       collect(s),s[2:5]), ('A','海','🌊',"StringIndexError",[1,2,5,9,10],['A','海','🌊','e','\u0301'],"海🌊"))
probe("text_firstindex", (s,""), (firstindex(s),firstindex("")),(1,1))
probe("text_lastindex", (s,""), (lastindex(s),lastindex("")),(10,0))
probe("text_length", (s,"e\u0301"), (length(s),length("e\u0301")),(5,2))
probe("text_ncodeunits", s, (ncodeunits(s),Int.(collect(codeunits(s)))),
      (11,[65,230,181,183,240,159,140,138,101,204,129]))
probe("text_codeunit", (s,3,6), (codeunit(s,3),codeunit(s,6),typeof(codeunit(s,3))), (0xb5,0x9f,UInt8))
probe("text_thisind", (s,3,8), (thisind(s,3),thisind(s,8)),(2,5))
probe("text_nextind", (s,2,2), (nextind(s,2),nextind(s,2,2),nextind(s,lastindex(s))),(5,9,12))
probe("text_prevind", (s,9,2), (prevind(s,9),prevind(s,9,2),prevind(s,firstindex(s))),(5,2,0))

part = "sector"
probe("text_concat", (label,part), (label*" / "*part,label,part,outcome(() -> label+part)),
      ("Harbor / sector","Harbor","sector","MethodError"))
probe("text_string", (label,7), string(label," #",7),"Harbor #7")
probe("text_repeat", ("NE ",3), ("NE "^3,repeat("NE ",3)),("NE NE NE ","NE NE NE "))
probe("text_interpolation", (label,part,[7,11],letter),
      ("$label $part","total=$(7+11)","data=$([7,11])","code=$letter","fee=\$8\n"),
      ("Harbor sector","total=18","data=[7, 11]","code=q","fee=\$8\n"))

block = """
    North
    South
  """
inline_close = """
    North
    South"""
probe("text_block", "opening newline and two indentation layouts",
      (block,inline_close),("  North\n  South\n","North\nSouth"))
probe("text_unicode_escapes", "code points and UTF-8 lengths",
      ('\u6d77'=='海','\U0001f30a'=='🌊',map(ncodeunits,("A","é","海","🌊"))),
      (true,true,(1,2,3,4)))
probe("text_compare", ("Bay","Harbor"), ("Bay"<"Harbor","Bay"=="Harbor","Bay"!="Harbor",
      "total=18"=="total=$(7+11)"), (true,false,true,true))
hay = "海A岸A"
probe("text_find_ends", (hay,'A','Q'),
      (findfirst('A',hay),findlast('A',hay),findfirst('Q',hay)), (4,8,nothing))
probe("text_find_from", (hay,5), (findnext('A',hay,5),findprev('A',hay,5)), (8,4))
probe("text_occurs", (hay,"海A",'岸'), (occursin("海A",hay),occursin("湾",hay),occursin('岸',hay)),
      (true,false,true))
probe("text_join", ("NE",["North","South","West"]),
      (repeat("NE",3),join(["North","South","West"],", "," and ")), ("NENENE","North, South and West"))
probe("text_startswith", label, (startswith(label,"Har"),startswith(label,"Bay")), (true,false))
probe("text_endswith", label, (endswith(label,"bor"),endswith(label,"Bay")), (true,false))
probe("text_strip", " \t海 \n", strip(" \t海 \n"),"海")
pieces = split("N,,S",',')
probe("text_split", "N,,S", (pieces,eltype(pieces),split("N,,S",',';keepempty=false)),
      (["N","","S"],SubString{String},["N","S"]))
probe("text_case", ("north","SOUTH"), (uppercase("north"),lowercase("SOUTH")),("NORTH","south"))
probe("text_raw", "newline, dollar, quote, and Windows path",
      ("N\nS",raw"N\nS",raw"fee=$limit",raw"\"",raw"E:\sample\files"),
      ("N\nS","N\\nS","fee=\$limit","\"","E:\\sample\\files"))
bytes = b"Harbor"
probe("text_byte_literal", "Harbor", (typeof(bytes),Int.(collect(bytes))),
      (Base.CodeUnits{UInt8,String},[72,97,114,98,111,114]))
probe("text_version", "1.10,1.11,1.10.13", (typeof(v"1.10.13"),v"1.10"<v"1.11",VERSION>=v"1.10"),
      (VersionNumber,true,true))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录

入口文件:projects/julia-math-text/text-strings.jl

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 text-strings.jl,或下载右侧课程工程包。
  2. 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Julia

课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip

下载课程关联工程包:Julia

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a211.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习