📄 string.jam
字号:
# (C) Copyright David Abrahams, 2002.
# (C) Copyright Rene Rivera, 2003.
#
# See accompanying license for terms and conditions of use.
#
import regex ;
# Characters considered whitespace, as a list.
.whitespace-chars = " " " " "
" ;
# Characters considered whitespace, as a single string.
.whitespace = $(.whitespace-chars:J="") ;
# Returns the canonical set of whitespace characters, as a list.
#
rule whitespace-chars ( )
{
return $(.whitespace-chars) ;
}
# Returns the canonical set of whitespace characters, as a single string.
#
rule whitespace ( )
{
return $(.whitespace) ;
}
# Splits the given string into a list of strings composed
# of each character of the string in sequence.
#
rule chars (
string # The string to split.
)
{
local result ;
while $(string)
{
local s = [ MATCH (.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.*) : $(string) ] ;
string = $(s[9]) ;
result += $(s[1-8]) ;
}
# trim off empty strings
while $(result[1]) && ! $(result[-1])
{
result = $(result[1--2]) ;
}
return $(result) ;
}
# Concatenates the given strings, inserting the given separator
# between each string.
#
rule join (
strings * # The strings to join.
: separator ? # The optional separator.
)
{
separator ?= "" ;
return $(strings:J=$(separator)) ;
}
# Split a string into whitespace separated words.
#
rule words (
string # The string to split.
: whitespace * # Optional, characters to consider as whitespace.
)
{
whitespace = $(whitespace:J="") ;
whitespace ?= $(.whitespace) ;
local w = ;
while $(string)
{
string = [ MATCH "^[$(whitespace)]*([^$(whitespace)]*)(.*)" : $(string) ] ;
if $(string[1]) && $(string[1]) != ""
{
w += $(string[1]) ;
}
string = $(string[2]) ;
}
return $(w) ;
}
# Check that the given string is composed entirely of whitespace.
#
rule is-whitespace (
string ? # The string to test.
)
{
if ! $(string) { return true ; }
else if $(string) = "" { return true ; }
else if [ MATCH "^([$(.whitespace)]+)$" : $(string) ] { return true ; }
else { return ; }
}
rule __test__ ( )
{
import assert ;
assert.result a b c : chars abc ;
# check boundary cases
assert.result a : chars a ;
assert.result : chars "" ;
assert.result a b c d e f g h : chars abcdefgh ;
assert.result a b c d e f g h i : chars abcdefghi ;
assert.result a b c d e f g h i j : chars abcdefghij ;
assert.result a b c d e f g h i j k : chars abcdefghijk ;
assert.result a//b/c/d : join a "" b c d : / ;
assert.result abcd : join a "" b c d ;
assert.result a b c : words "a b c" ;
assert.true is-whitespace " " ;
assert.false is-whitespace " a b c " ;
assert.true is-whitespace "" ;
assert.true is-whitespace ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -