Counter strings are self-documenting strings consisting of character position references and markers. Each numeric character position reference indicates the string length at the following marker. The primary application for counter strings is in testing input to text fields. Given the nature of counter strings it is immediately apparent when and where truncation has occurred on a string.
Take, for example, this 64-character counter string:
"*3*5*7*10*13*16*19*22*25*28*31*34*37*40*43*46*49*52*55*58*61*64*"
If it had been truncated at 32 characters, it would appear like this:
"*3*5*7*10*13*16*19*22*25*28*31*3"
(require counter-string) package: counter-string
(counter-string [length #:mark marker]) -> string?
length : exact-positive-integer? = 256
marker : non-empty-string? = "*"
Returns a counter string of length
characters, where character
position indicators are terminated by marker
.
Examples:
> (counter-string)
"*3*5*7*9*12*15*18*21*24*27*30*33*36*39*42*45*48*51*54*57*60*63*66*69*72*75*78*81*84*87*90*93*96*100*104*108*112*116*120*124*128*132*136*140*144*148*152*156*160*164*168*172*176*180*184*188*192*196*200*204*208*212*216*220*224*228*232*236*240*244*248*252*256*"
> (counter-string 30)
"*3*5*7*9*12*15*18*21*24*27*30*"
> (counter-string #:mark "^")
"^3^5^7^9^12^15^18^21^24^27^30^33^36^39^42^45^48^51^54^57^60^63^66^69^72^75^78^81^84^87^90^93^96^100^104^108^112^116^120^124^128^132^136^140^144^148^152^156^160^164^168^172^176^180^184^188^192^196^200^204^208^212^216^220^224^228^232^236^240^244^248^252^256^"
> (counter-string 30 #:mark "_")
"_3_5_7_9_12_15_18_21_24_27_30_"
A multi-character marker
is supported. The position indicated is that
of the final character within the marker string.
; Just... no. But go ahead if you must.
> (counter-string 24 #:mark "<(ಠ_ಠ)>")
"<<<<<<15<(ಠ_ಠ)>24<(ಠ_ಠ)>"
The command line tool, counter-string.rkt
, is found in the collection’s
bin
directory and is simply a wrapper around the counter-string
function.
counter-string [ <option> ... ] [<length>] where <length> is a positive integer : Default: 256. where <option> is one of --marker <marker>, -m <marker> : Default: * Specify the marker character. --help, -h : Show this help -- : Do not treat any remaining argument as a switch (at this level) Multiple single-letter switches can be combined after one `-'; for example: ~-h-' is the same as ~-h --' Creates a <length> characters long counter string. Each integer indicates the character position of the following <marker>. Ex. 24 character counter-string: *3*5*7*9*12*15*18*21*24*
For the sake of those with CLI allergies, I threw together a GUI:
counter-string-gui.rkt
. It is found in the collection’s bin
directory.
If you have Racket installed, building executables is very easy. Here’s how I built executables for my Windows using co-workers:
$ cd bin $ raco exe counter-string.rkt $ raco exe counter-string-gui.rkt $ raco distribute build counter-string.exe counter-string-gui.exe $ tree -L 1 build build ├── counter-string.exe ├── counter-string-gui.exe └── lib 1 directory, 2 files
Move the build
directory wherever you want it, rename it to counter-string
,
and add it to %PATH%
. Note, also, that you could build it to whatever
executable names you like. See the -o
argument for raco
.
James Bach describes counter strings and offeres his PerlClip program on his blog: Counterstrings: Self-Describing Test Data. PerlClip is still available as both a Perl script and a compiled Windows executable, but it is not suitable for command line usage where the use of standard output is expected.