Page 1 of 1

six case-insensitive comparison functions

Posted: 24 Jul 2019, 10:49
by jeeswg
First! (On the AutoHotkey v2 Scripts and Functions forum.)

- Whenever I start using a new programming language, I always want 6 specific case-insensitive comparison functions (comparators).
- I haven't come across anyone discussing these, so any links would be appreciated.
- Also, I've had to invent some names, but any name suggestions would be interesting.

Code: Select all

q:: ;sort text using different case-insensitive comparison functions
vDelim := Chr(1)
vItem := 3
if (vItem = 1)
	vText := "ABCabcdefDEF"
if (vItem = 2)
	vText := "FEDfedcbaCBA"
if (vItem = 3) ;characters 33 to 126
{
	vText := ""
	Loop 126
	{
		if (A_Index >= 33)
			vText .= Chr(A_Index)
	}
}

vOutput := "orig`t" vText "`r`n"
vText := RegExReplace(vText, "(?<=.)(?=.)", vDelim)
for _, vValue in StrSplit("Upper,UpperUF,UpperLF,Lower,LowerUF,LowerLF", ",")
	vOutput .= vValue "`t" StrReplace(Sort(vText, "D" vDelim, "JEE_StrCmp" vValue), vDelim) "`r`n"
Clipboard := vOutput
MsgBox("done")
return

;==================================================

;CmpUpper: convert to upper then compare
;CmpUpperUF: convert to upper then compare, if equal, compare originals case sensitive (upper goes first)
;CmpUpperLF: convert to upper then compare, if equal, compare originals case sensitive (lower goes first)
;CmpLower: convert to lower then compare
;CmpLowerUF: convert to lower then compare, if equal, compare originals case sensitive (upper goes first)
;CmpLowerLF: convert to lower then compare, if equal, compare originals case sensitive (lower goes first)

;==================================================

JEE_StrCmpUpper(vTextA, vTextB, vOffset:=0)
{
	local
	if !(vRet := StrCompare(StrUpper(vTextA), StrUpper(vTextB), 1))
		vRet := -vOffset
	return vRet
}

JEE_StrCmpUpperUF(vTextA, vTextB, vOffset:=0)
{
	local
	if !(vRet := StrCompare(StrUpper(vTextA), StrUpper(vTextB), 1))
	&& !(vRet := StrCompare(vTextA, vTextB, 1))
		vRet := -vOffset
	return vRet
}

JEE_StrCmpUpperLF(vTextA, vTextB, vOffset:=0)
{
	local
	if !(vRet := StrCompare(StrUpper(vTextA), StrUpper(vTextB), 1))
	&& !(vRet := -StrCompare(vTextA, vTextB, 1)) ;deliberate minus sign
		vRet := -vOffset
	return vRet
}

JEE_StrCmpLower(vTextA, vTextB, vOffset:=0)
{
	local
	if !(vRet := StrCompare(StrLower(vTextA), StrLower(vTextB), 1))
		vRet := -vOffset
	return vRet
}

JEE_StrCmpLowerUF(vTextA, vTextB, vOffset:=0)
{
	local
	if !(vRet := StrCompare(StrLower(vTextA), StrLower(vTextB), 1))
	&& !(vRet := StrCompare(vTextA, vTextB, 1))
		vRet := -vOffset
	return vRet
}

JEE_StrCmpLowerLF(vTextA, vTextB, vOffset:=0)
{
	local
	if !(vRet := StrCompare(StrLower(vTextA), StrLower(vTextB), 1))
	&& !(vRet := -StrCompare(vTextA, vTextB, 1)) ;deliberate minus sign
		vRet := -vOffset
	return vRet
}

;==================================================
- Example output:

Code: Select all

orig	ABCabcdefDEF
Upper	AaBbCcdDeEfF
UpperUF	AaBbCcDdEeFf
UpperLF	aAbBcCdDeEfF
Lower	AaBbCcdDeEfF
LowerUF	AaBbCcDdEeFf
LowerLF	aAbBcCdDeEfF

orig	FEDfedcbaCBA
Upper	aAbBcCDdEeFf
UpperUF	AaBbCcDdEeFf
UpperLF	aAbBcCdDeEfF
Lower	aAbBcCDdEeFf
LowerUF	AaBbCcDdEeFf
LowerLF	aAbBcCdDeEfF

orig	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Upper	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`{|}~
UpperUF	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`{|}~
UpperLF	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`{|}~
Lower	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz{|}~
LowerUF	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz{|}~
LowerLF	!"#$%&'()*+,-./0123456789:;<=>[email protected][\]^_`aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ{|}~
[EDIT: Renamed from JEE_CmpXXX to JEE_StrCmpXXX.]

Re: six case-insensitive comparison functions

Posted: 24 Jul 2019, 12:45
by Helgef
Hi jeeswg :wave:.
Good job, thanks for sharing.

Cheers.

Your functions require a least [a101].

OT:
Spoiler