191 lines · 4.4 KB · Text
RawDownload
#!/bin/rcecho T.utf: tests of utf functions $awk 'BEGIN {	FS = "\t"	awk = ENVIRON["awk"]}NF == 0 || $1 ~ /^#/ {	next}$1 ~ /try/ {	# new test	nt++	sub(/try [a-zA-Z_0-9]+ /, "")	prog = $0	# printf("try %3d %s\n", nt, prog)	prog = sprintf("%s -F''\\t'' ''%s''", awk, prog)	# print "prog is", prog	nt2 = 0	while (getline > 0) {		if (NF == 0)	# blank line terminates a sequence			break		input = $1		for (i = 2; i < NF; i++)	# input data			input = input "\t" $i		test = sprintf("echo ''%s'' | %s >foo1; ",			input, prog)		if ($NF == "\"\"")			output = ">foo2;"		else			output = sprintf("echo ''%s'' >foo2; ", $NF)		gsub(/\\t/, "\t", output)		gsub(/\\n/, "\n", output)		run = sprintf("diff foo1 foo2 || echo test %d.%d failed",			nt, ++nt2)		# print  "input is", input		# print  "test is", test		# print  "output is", output		# print  "run is", run		system(test output run)	}	tt += nt2}#END { print tt, "tests" }' <<'!!!!'# General format:# try program as rest of line# $1	$2	$3	output1  (\t for tab, \n for newline,# $1	$2	$3	output2  ("" for null)# ... terminated by blank line # try another program... try length { print length($1) }	0a	1の今がその時だ	7Сейчас	6现在是时候了	6给所有的好男	6来参加聚会。	6😀	1🖕 finger	8Τωρα	4για	3να	2עכשיו	5לכל	3לבוא	4の今がその時だ	7지금이	3모든	2파티에	3Сейчас	6для	3прийти	6 try index { print index($1, $2) }abc	a	1abc	b	2abc	x	0现在是时候了	""	0现在是时候了	了	6现在是时候了	在是	2现在是时候了	x	0现x在是时候了	x	2🖕 fingerすべての善人のためにすべての善人のために	f	3🖕 finger🖕	r🖕	8 try substr { print substr($0, 2, 3) }abcdef	bcdΤωρα ειναι η	ωραΤω	ω지금 이절호의	금 이xпyрийти	пyр try rematch { print $1 ~ $2 }abc	a	1abc	x	0すべての善人のために	の	1すべての善人のために	の.*の	1すべての善人のために	の.*て	0Τωρα	ω+	1 # replace first occurrence of $2 by $3 in $1try sub { n = sub($2, $3, $1); print n, $1 }abcdef	bc	XYZ	1 aXYZdefabcdef	xy	XYZ	0 abcdefの今がその時だ	の	NO	1 NO今がその時だ🖕 finger	🖕.*g	FING	1 FINGerСейчас	.	x	1 xейчас # replace all occurrences of $2 by $3 in $1try gsub { n = gsub($2, $3, $1); print n, $1 }abcdef	bc	XYZ	1 aXYZdefabcdef	xy	XYZ	0 abcdefの今がその時だ	の	NO	2 NO今がそNO時だ🖕 finger	🖕.*g	FING	1 FINGerСейчас	.	x	6 xxxxxx try match { print match($1, $2), RSTART, RLENGTH }abc	[^a]	2 2 1abc	[^ab]	3 3 1すべての善人のために	[^す]	2 2 1すべての善人のために	[^ぁ-ゖ]	5 5 1abc	a	1 1 1abc	x	0 0 -1すべての善人のために	の	4 4 1すべての善人のために	の.*の	4 4 4すべての善人のために	の.*て	0 0 -1Τωρα	ω+	2 2 1Τωρα	x+	0 0 -1Τωρα	ω.	2 2 2すべての善人のために	[の]	4 4 1すべての善人のために	[ぁ-え]	0 0 -1すべての善人のために	[^ぁ-え]	1 1 1Τωρα ειναι η	[α-ω]	2 2 1Τωρα ειναι η	[α-ω]+	2 2 3xxxΤωρα ειναι η	[Α-Ω]	4 4 1για όλους τους καλούς ά	α.*α	3 3 15να έρθει στο πά	[^ν]	2 2 1 # FS="" should split into unicode charstry emptyFS BEGIN {FS=""} {print NF}すべての善人のために	10の今がその時だ	7Сейчас	6现在是时候了	6给所有的好男	6来参加聚会。	6😀	1🖕 finger	8 # printf(%N.Ns) for utf8 stringstry printfs1 {printf("[%5.2s][%-5.2s]\n"), $1, $1}abcd	[   ab][ab   ]现在abc	[   现在][现在   ]现ωabc	[   现ω][现ω   ]ωabc	[   ωa][ωa   ]Сейчас	[   Се][Се   ]Сейxyz	[   Се][Се   ]😀	[    😀][😀    ] # printf(%N.Ns) for utf8 stringstry printfs2 {printf("[%5s][%-5s]\n"), $1, $1}abcd	[ abcd][abcd ]现在ab	[ 现在ab][现在ab ]a现在ab	[a现在ab][a现在ab]a现在abc	[a现在abc][a现在abc]现ωab	[ 现ωab][现ωab ]ωabc	[ ωabc][ωabc ]Сейчас	[Сейчас][Сейчас]😀	[    😀][😀    ] # printf(%N.Ns) for utf8 stringstry printfs3 {printf("[%.2s][%-.2s]\n"), $1, $1}abcd	[ab][ab]现在abc	[现在][现在]现ωabc	[现ω][现ω]ω	[ω][ω]😀	[😀][😀] # printf(%c) for utftry printfc {printf("%c %c\n", $1, substr($1,2,1))}すべての善人のために	す べの今がその時だ	の 今Сейчас	С е现在是时候了	现 在😀🖕	😀 🖕 !!!!