很多程式語言基本上都會提供三個單或雙引號來支援區塊(block)字串,例如 Python 就有提供相關功能。然而在 Parsing 上,Julia 卻是有一套規則在運行(部分測試也與 Python 有不同的結果),以下舉一些測試範例,以及大略推測 Julia 是如何解析:
Ref:
Julia v1.0 Docs - Triple-Quoted String Literals
我是看了文件了,但是不知道是不是自己資質駑鈍看不懂他的英文在寫啥。
Stackoverflow - Does the first line of triple-quoted string impact identation?
Github Issue - Julia - Problem with parsing triple quoted strings
排版歸依
例 A:
julia> str = """
Hello,
world.
"""
" Hello,\n world.\n"
推測行為:
# 原始
str = """
Hello,
world.
"""
# 斷第一行
"""
Hello,
world.
"""
# 去除第一個新行字元 (Leading Newline)
""" Hello,
world.
"""
# 去除縮排,補上新行字元 (Newline)
""" Hello,\n world.\n """
(4) (4) (2)
# 縮共同最長起始空白 (2格)
""" Hello,\n world.\n"""
#最後變成
" Hello,\n world.\n"
例 B:
julia> str = """
Hello,
world.
"""
" Hello,\n world.\n"
推測行為:
# 原始
str = """
Hello,
world.
"""
# 斷第一行
str =
"""
Hello,
world.
"""
# 去除第一個新行字元(Leading Newline)
""" Hello,
world.
"""
# 去除縮排,補上新行字元(Newline)
""" Hello,\n world.\n """
(4) (4) (3)
# 縮共同最長起始空白(3格)
""" Hello,\n world.\n"""
#最後變成
" Hello,\n world.\n"
例 C:
julia> str = """
Hello,
world.
"""
"Hello,\nworld.\n"
推測行為:
# 原始
str = """
Hello,
world.
"""
# 斷第一行
"""
Hello,
world.
"""
# 去除第一個新行字元(Leading Newline)
""" Hello,
world.
"""
# 去除縮排,補上新行字元(Newline)
""" Hello,\n world.\n """
(4) (4) (4)
# 縮共同最長起始空白(4格)
"""Hello,\nworld.\n"""
#最後變成
"Hello,\nworld.\n"
例 D:
julia> str = """
Hello,
world.
"""
" Hello,\n world.\n"
推測行為:
# 原始
str = """
Hello,
world.
"""
# 斷第一行
"""
Hello,
world.
"""
# 去除第一個新行字元(Leading Newline)
""" Hello,
world.
"""
# 去除縮排,補上新行字元(Newline)
""" Hello,\n world.\n """
空白: (11) (10) (9)
# 縮共同最長起始空白(9格)
""" Hello,\n world.\n"""
#最後變成
" Hello,\n world.\n"
例 E:
julia> str = """
Hello,
world.
aa"""
" Hello,\n world.\naa"
推測行為:
# 原始
str = """
Hello,
world.
aa"""
# 斷第一行
"""
Hello,
world.
aa"""
# 去除第一個新行字元(Leading Newline)
""" Hello,
world.
aa"""
# 去除縮排,補上新行字元(Newline)
""" Hello,\n world.\n aa"""
空白: (11) (10) (4)
# 縮共同最長起始空白(4格)
""" Hello,\n world.\naa"""
#最後變成
" Hello,\n world.\naa"
例 F:
julia> str = """
Hello,
world.
aa """
"Hello,\n world.\n aa "
推測行為:
# 原始
str = """
Hello,
world.
aa """
# 斷第一行
"""
Hello,
world.
aa """
# 去除第一個新行字元(Leading Newline)
""" Hello,
world.
aa """
# 去除縮排,補上新行字元(Newline)
""" Hello,\n world.\n aa """
空白: (7) (8) (12) ^(非起始空白)
# 縮共同最長起始空白(7格)
"""Hello,\n world.\n aa """
#最後變成
"Hello,\n world.\n aa "
例 G:
julia> str = """
Hello,
world.
aa
"""
"Hello,\n world.\n aa\n "
推測行為:
# 原始
str = """
Hello,
world.
aa
"""
# 斷第一行
"""
Hello,
world.
aa
"""
# 去除第一個新行字元(Leading Newline)
""" Hello,
world.
aa
"""
# 去除縮排,補上新行字元(Newline)
""" Hello,\n world.\n aa\n """
空白: (7) (8) (12) (9)
# 縮共同最長起始空白(7格)
"""Hello,\n world.\n aa\n """
#最後變成
"Hello,\n world.\n aa\n "
後記:
Julia 只接受 """..."""
,不理你的'''...'''
。
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
Pinterest
Email