malko |
04 November 2021 21:50 |
AmigaOS 1.3 'ccase' command
Have you ever been in the need to change the case of a string ?
You know, you display things on the screen and you want that the input of the user to be correctly formatted... (users may be distracted :p ...)
This is exactly what the 'ccase' (Change Case) command does.
Three case choices are available :
- " L " switch forces lowercase
- " U " switch forces uppercase
- no switch simply invert the letters case
A QUIET switch is also available for the command to be silent
Of course, strings with space have to be surrounded by quotation marks.
Please mind that only a-z letters are processed. Others characters, like characters with accents, are left untouched.
As usual the result is available in a variable (cCASE<shell_number>) and, cherry on the cake, the string length is also available (cLNGR<shell_number> - not that it is useful here, but the value was available and it costed nothing to make it available through a variable).
NEW VERSION OF THE SCRIPT AVAILABLE BELOW. SEE POST #2
Code:
.key STRING/A,U/S,L/S,QUIET/S
.bra {
.ket }
;---------------------
;initialisation
;-
setenv cccptA{$$} 1
setenv cccptB{$$} 0
setenv ccstr0{$$} "{STRING}"
echo >env:ccstr9{$$}t "" NOLINE
echo >t:rmcr{$$} "1CL//;w"
echo >t:rmet{$$} "pa l//;<;sa//;p;d;w"
echo >t:rmhx{$$} "pa/0A/;2<;sa//;d;p;pa l//;2<;sa//;p;d;A//0x/;w"
;---------------------
;début de boucle : preparation de l'extraction
;-
lab mlk01
setenv <env:cccptA{$$} >NIL: echop{$$}t ?
echo >>env:echop{$$}t " LEN 1 *"{STRING}*"" NOLINE
echo >env:echop{$$} "FIRST " NOLINE
type >>env:echop{$$} env:echop{$$}t
;---------------------
;extraction par caractère & nettoyage
;-
echo <env:echop{$$} >env:exstr1{$$}t ?
edit >NIL: FROM env:exstr1{$$}t TO env:exstr1{$$} WITH t:rmet{$$}
;---------------------
;valeur hexadécimale du caractère extrait & nettoyage
; miniscules: a=0x61=97 ; z=0x7A=122
; majuscules: A=0x41=65 ; Z=0x5A=90
;-
type >env:vhstr1{$$}t env:exstr1{$$} OPT h
edit >NIL: FROM env:vhstr1{$$}t TO env:vhstr1{$$} WITH t:rmhx{$$}
;---------------------
;valeur décimale du caractère extrait
;-
eval <env:vhstr1{$$} >NIL: TO env:vdstr1{$$} lformat %N ?
;---------------------
;test de case
;-
if VAL $vdstr1{$$} GE 97
if VAL $vdstr1{$$} NOT GT 122
if "{L}" NOT EQ "L"
skip min2maj
endif
endif
endif
if VAL $vdstr1{$$} GE 65
if VAL $vdstr1{$$} NOT GT 90
if "{U}" NOT EQ "U"
skip maj2min
endif
endif
endif
;---------------------
;on garde tel quel
;-
if VAL $vdstr1{$$} EQ 0
setenv vhstr1{$$} "0x00"
setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
echo >>env:evalp{$$} " + 0x20" NOLINE
else
setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
echo >>env:evalp{$$} " + 0x00" NOLINE
endif
skip mlk02
quit
lab min2maj
;---------------------
;préparation du changement de case (minuscule à majuscule)
;-
setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
echo >>env:evalp{$$} " - 0x20" NOLINE
skip mlk02
lab maj2min
;---------------------
;préparation du changement de case (majuscule à minuscule)
;-
setenv <env:vhstr1{$$} >NIL: evalp{$$} ?
echo >>env:evalp{$$} " + 0x20" NOLINE
skip mlk02
lab mlk02
;---------------------
;conversion de case (minuscule <-> majuscule)
;-
eval <env:evalp{$$} >NIL: TO env:cvstr1{$$} lformat "%C" ?
;---------------------
;concaténation de la chaîne convertie
;-
join env:ccstr9{$$}t "env:cvstr1{$$}" AS env:ccstr9{$$}
copy env:ccstr9{$$} env:ccstr9{$$}t CLONE
lab mlk03
;---------------------
;la chaîne originale et celle recréée sont-elles identiques ?
;-
if $ccstr9{$$} NOT EQ $ccstr0{$$}
;---------------------
;incrémentation du compteur
;-
eval <env:cccptA{$$} >NIL: TO VALUE2 1 OP + TO env:cccptB{$$} ?
setenv <env:cccptB{$$} >NIL: cccptA{$$} ?
skip mlk01 BACK
endif
;---------------------
;fin
;-
copy env:ccstr9{$$} TO env:cCASE{$$} CLONE QUIET
setenv <env:cccptA{$$} >NIL: cLNGR{$$} ?
if "{QUIET}" NOT EQ "QUIET"
echo " *e[32m$cCASE{$$} : " NOLINE
getenv cCASE{$$}
echo "*e[0m" NOLINE
echo " *e[32m$cLNGR{$$} : " NOLINE
getenv cLNGR{$$}
echo "*e[0m"
endif
run >NIL: delete t:rm(cr|et|hx){$$} env:cccpt(A|B){$$} env:(cc|cv|ex|vd|vh)str(0|1|9){$$}#? env:echop{$$}#? env:evalp{$$}#? QUIET
quit 0
And how it looks :
https://i.ibb.co/vZJgfkB/ccase.gif
|