six case-insensitive comparison functions

Post your working scripts, libraries and tools.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

six case-insensitive comparison functions

24 Jul 2019, 10:49

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:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Upper	!"#$%&'()*+,-./0123456789:;<=>?@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz[\]^_`{|}~
UpperUF	!"#$%&'()*+,-./0123456789:;<=>?@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz[\]^_`{|}~
UpperLF	!"#$%&'()*+,-./0123456789:;<=>?@aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ[\]^_`{|}~
Lower	!"#$%&'()*+,-./0123456789:;<=>?@[\]^_`AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz{|}~
LowerUF	!"#$%&'()*+,-./0123456789:;<=>?@[\]^_`AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz{|}~
LowerLF	!"#$%&'()*+,-./0123456789:;<=>?@[\]^_`aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ{|}~
[EDIT: Renamed from JEE_CmpXXX to JEE_StrCmpXXX.]
Last edited by jeeswg on 24 Aug 2019, 06:07, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: six case-insensitive comparison functions

24 Jul 2019, 12:45

Hi jeeswg :wave:.
Good job, thanks for sharing.

Cheers.

Your functions require a least [a101].

OT:
Spoiler

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: lone525 and 15 guests