summaryrefslogtreecommitdiffstats
path: root/eshtrans/split.sh
blob: bfc51cd6b3dab99a4cbb66f2ac5820a65e8df2d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
set -u

buf="Hello, 'world'!"

old()
{
	eval "$(printf '%s' "${buf}" | awk -v FS='' -v j=0 \
		-v squote="'" -v esc_squote="'\\\\''" '
		{
			for (i = 1; i <= NF; ++i) {
				sub(squote, esc_squote, $i);
				printf("lbufv_%d='\''%s'\''\n", j++, $i);
			};
			printf("lbufv_%d='\''\n'\''\n", j++);
		}
		')"
}

new()
{
	eval "$(printf '%s' "${buf}" | awk -v FS='' -v j=0 \
		-v squote="'" -v esc_squote="'\\\\''" '
		{
			for (i = 1; i <= NF; ++i) {
				sub(squote, esc_squote, $i);
				printf("lbufv_%d=" squote "%s" squote "\n",
					j++, $i);
			};
			printf("lbufv_%d=" squote "\n" squote "\n", j++);
		}
		')"
}

print_and_clear()
{
	i=0
	while [ ${i} -le ${#buf} ]; do
		c=$(eval "printf '%sx' \"\${lbufv_${i}}\"")
		eval "lbufv_${i}="
		printf 'i: %2d, c: "%s"\n' ${i} "${c%x}"
		i=$((${i} + 1))
	done
}

old
print_and_clear
new
print_and_clear