ycl

Check-in [c030a0ad2b]
Login

Check-in [c030a0ad2b]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:{ycl string} {redesign of [template]}
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c030a0ad2b50d88d92eb29504ed703665d708da0
User & Date: pooryorick 2017-08-05 21:29:19.671
Context
2017-08-06
04:57
{ycl coro call} {rename [called] to [callcmd]} check-in: d4f770768f user: pooryorick tags: trunk
2017-08-05
21:29
{ycl string} {redesign of [template]} check-in: c030a0ad2b user: pooryorick tags: trunk
20:32
{ycl coro call} {refactor [reply]} check-in: 59c9a72c4e user: pooryorick tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to packages/string/lib/string.tcl.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /bin/env tclsh

namespace import ::tcl::mathfunc::max
namespace import ::tcl::mathfunc::min
namespace import ::tcl::mathop::+
namespace import ::tcl::mathop::-
namespace import ::tcl::mathop::/
namespace import ::tcl::mathop::<<
package require {ycl coro call}
namespace import [yclprefix]::coro::call::bye
namespace import [yclprefix]::coro::call::reply
package require {ycl proc}
namespace import [yclprefix]::proc::checkargs
package require {ycl list}
interp alias {} [namespace current]::all {} [yclprefix] list all
variable ldedent [yclprefix]::list::dedent

namespace eval doc {}








<
<
<







1
2
3
4
5
6
7
8



9
10
11
12
13
14
15
#! /bin/env tclsh

namespace import ::tcl::mathfunc::max
namespace import ::tcl::mathfunc::min
namespace import ::tcl::mathop::+
namespace import ::tcl::mathop::-
namespace import ::tcl::mathop::/
namespace import ::tcl::mathop::<<



package require {ycl proc}
namespace import [yclprefix]::proc::checkargs
package require {ycl list}
interp alias {} [namespace current]::all {} [yclprefix] list all
variable ldedent [yclprefix]::list::dedent

namespace eval doc {}
249
250
251
252
253
254
255




256
257
258
259
260
261
262
263
264
265
266

267
268
269
270
271
272
273
    if {![catch {expr {abs($value)}}]} {
        return 1
    }
    return 0
}

proc iter value {




	set name [namespace current]::[info cmdcount]
	set res [coroutine $name ::apply [list value {
		set length [string length $value]
		reply
		for {set i 0} {$i < $length} {incr i} {
			reply [string index $value $i]
		}
		bye
	} [namespace current]] $value]
	return $name


}

variable doc::pstring {
	description {
		convert non-printing characters in $string into their \x or \u escaped
		form 
	}







>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
>







246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    if {![catch {expr {abs($value)}}]} {
        return 1
    }
    return 0
}

proc iter value {
	package require {ycl coro call}
	namespace import [yclprefix]::coro::call::bye
	namespace import [yclprefix]::coro::call::reply
	proc iter value {
		set name [namespace current]::[info cmdcount]
		set res [coroutine $name ::apply [list value {
			set length [string length $value]
			reply
			for {set i 0} {$i < $length} {incr i} {
				reply [string index $value $i]
			}
			bye
		} [namespace current]] $value]
		return $name
	}
	tailcall iter $value
}

variable doc::pstring {
	description {
		convert non-printing characters in $string into their \x or \u escaped
		form 
	}
339
340
341
342
343
344
345



346
347




348


349



350


351


352
353
354
355
356
357
358







359











































360



361


























362
363
364
365
366
367
368
369
370
	set text [split $text \n]
	set text [$ldedent $text]
	set text [join $text[set text [list]] \n]
	return $text
}

variable doc::template {



	description {
		like [string map], but more concise.  All mapped values are quoted with




		[list].  To unquote a substituted value, do something like set it to a


		variable value and then use an [eval] context to unquote.



	}


}


proc template {delimiter args} {
	if {![llength $args]} {
		error [list {wrong # args} [llength $args]]
	}
	set string [lindex $args end]
	set args [lrange $args[set args {}] 0 end-1]
	set script {::string map [list } 







	foreach arg $args {











































		append script "$delimiter$arg$delimiter \[list \$$arg] "



	}


























	append script {] } [list $string]
	uplevel 1 $script 
}

proc COUNT {} {return count}
proc INDEXES {} {return indexes}
proc INFO {} {return info}
proc STRINGS {} {return strings}








>
>
>

|
>
>
>
>
|
>
>
|
>
>
>
|
>
>
|
>
>
|






>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|






341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
	set text [split $text \n]
	set text [$ldedent $text]
	set text [join $text[set text [list]] \n]
	return $text
}

variable doc::template {
	synopsis {
		template  ?varspec ...?  string
	}
	description {
		A concise way to invoke [string map] . All mapped values are quoted
		with [list] . Each $varspec is the name of a variable , optionally
		preceded by a delimiter that ends with a character that isn't valid in
		a variable name , and optionally followed by a delimiter that begins
		with a character which isn't valid in a variable name .

		If $varspec is "#" , it signifies that the subsequent $varspec is not to
		be quoted with [list] .

		If $varspec is "=" , it signifies that the subsequent $varspec is list
		containing the $odelim and optionally the $cdelim for the $varspec that
		follows it . If only $odelim is provided , $cdelim shares its value .

		A $varspec value of  "!" is like "=", but its effects remain in place
		for the all the following $varspec values.

	}
}
proc template args [string map [list @varchars@ {a-zA-z0-9_}] {
	if {![llength $args]} {
		error [list {wrong # args} [llength $args]]
	}
	set string [lindex $args end]
	set args [lrange $args[set args {}] 0 end-1]
	set script {::string map [list } 
	set state {}
	set changedefaultdelim 0
	set changedelim 0
	set list 1
	set odefault @
	set cdefault @
	set mode varspec
	foreach arg $args {
		switch $state {
			{} {
				switch $arg {
					= {
						set state changedelim
						set mode varname
						continue
					}
					! {
						set state changedefaultdelim
						set mode varspec
						continue
					}
					\# {
						set list 0
						continue
					}
				}
			}
			changedelim {
				set arg [lassign $arg[set arg {}] odelim]
				if {[llength $arg]} {
					lassign $arg cdelim
				}
				set changedelim 1
				set state {}
				continue
			}
			changedefaultdelim {
				set arg [lassign $arg[set arg {}] odefault]
				if {[llength $arg]} {
					lassign $arg cdefault
				} else {
					set cdefault $odefault
				}
				set changedefaultdelim 0
				set state {}
				continue
			}
			default {
				error [list {unknown state} $state]
			}
		}

		if {!$changedelim} {
			set odelim $odefault 
			set cdelim $cdefault
		}
		if {$mode eq {varspec}} {
			if {![regexp "^(.*\[^@varchars@])?(\[@varchars@]+|\[@varchars@]*\\(\[^\\)]*\\))(\[^a-zA-z0-9_].*)?$" \
				$arg -> odelim1 varname cdelim1]} {

				error [list {bad varspec} $arg]
			}
			if {$odelim1 ne {}} {
				set odelim $odelim1
			}
			if {$cdelim1 ne {}} {
				set cdelim $cdelim1
			}
		} else {
			set varname $arg
		}
		if {$list} {
			set qvarname "\[list \$$varname]"
	 	} else {
			set qvarname \$$varname
		}
		append script "$odelim$varname$cdelim $qvarname "
		set list 1
		set changedefaultdelim 0
		set changedelim 0
		set mode varspec
	}
	append script {] } [list $string]
	uplevel 1 $script 
}]

proc COUNT {} {return count}
proc INDEXES {} {return indexes}
proc INFO {} {return info}
proc STRINGS {} {return strings}

Changes to packages/string/lib/string.test.tcl.
13
14
15
16
17
18
19

20
21
22
23
24
25
26
	namespace import [yclprefix]::test::cleanup1
	package require {ycl test data}
	namespace import [yclprefix]::test::data
	package require {ycl string}
	namespace import [yclprefix]::string::cmp
	namespace import [yclprefix]::string::shortmatch
	namespace import [yclprefix]::string::delimit


	test cmp1 {} -body {
		cmp hello hello
	} -result -1

	test cmp2 {} -body {
		cmp hello heplo







>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	namespace import [yclprefix]::test::cleanup1
	package require {ycl test data}
	namespace import [yclprefix]::test::data
	package require {ycl string}
	namespace import [yclprefix]::string::cmp
	namespace import [yclprefix]::string::shortmatch
	namespace import [yclprefix]::string::delimit
	namespace import [yclprefix]::string::template

	test cmp1 {} -body {
		cmp hello hello
	} -result -1

	test cmp2 {} -body {
		cmp hello heplo
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

	test iter {} -body {
		set string banana
		set iter [ycl string iter $string]
		while 1 {
			lappend res [call $iter]
		}
		puts [list alldone]
		return $res
	} -cleanup [cleanup1] -result [sl {
		b a n a n a
	}]

	test shortmatch_one_match {} -body {
		shortmatch *t* t







<







73
74
75
76
77
78
79

80
81
82
83
84
85
86

	test iter {} -body {
		set string banana
		set iter [ycl string iter $string]
		while 1 {
			lappend res [call $iter]
		}

		return $res
	} -cleanup [cleanup1] -result [sl {
		b a n a n a
	}]

	test shortmatch_one_match {} -body {
		shortmatch *t* t
183
184
185
186
187
188
189



























190
191
192
193
194
195
196
197
198
199
200
	test delimit_count {} -body {
		delimit $quote1 string {d } format count
	} -result {2}

	test delimit_count_zero {} -body {
		delimit $quote1 string {xzd } format count
	} -result {0}




























	cleanupTests
}

variable cleanup1 {
	apply {{} {
		foreach name {res} {
			catch {unset $name}
		}
	}}
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	test delimit_count {} -body {
		delimit $quote1 string {d } format count
	} -result {2}

	test delimit_count_zero {} -body {
		delimit $quote1 string {xzd } format count
	} -result {0}

	test template {} -body {
		set a 5
		set b 7
		set expr {[set b]}
		::apply [list {} [template a b # expr {
			set a @a@
			set b @b@
			expr {$a + @expr@}
		}]]
	} -result 12

	test template_customdelim {} -body {
		set a 5
		set b 7
		set c 3
		set d 2
		set e 1
		set expr {[set b]}
		::apply [list {} [template a !b* # &expr> c ! {` )} d e {
			set a @a@
			set b !b*
			set c @c@
			set d `d)
			expr {$a + &expr> + $c + $d + `e)}
		}]]
	} -result 18 

	cleanupTests
}

variable cleanup1 {
	apply {{} {
		foreach name {res} {
			catch {unset $name}
		}
	}}
}