I define a "list" as a delimited string like "1,2,3" or "A|B|C" or even "The quick brown fox..."(space is the delimiter", etc.
For a string the best I can think of is this:(Only really tested with ANSI, but should work since the offset is 0-based.) (Not the best function, look at LinearSpoon below.)
Code: Select all
reverse_string(ByRef p_In) { ; Byref is more efficient with larger strings.
iLen := StrLen(p_In) ; Get the number of characters.
; ANSI is 1 byte characters, Unicode is 2-byte
dType := A_IsUnicode?"UShort":"UChar"
dLen := A_IsUnicode?2:1
; Set the capacity of the output string.
VarSetCapacity(sOut, (iLen + 1) * dLen, 0) ; Add 1 to allocate a null terminator.
while (A_Index <= iLen) ; Loop through each character.
{ V := NumGet(p_In, (iLen - A_Index) * dLen, dType) ; Get each character from memory.
NumPut(V, sOut, (A_Index-1)*dLen, dType) ; Write to the opposite end of the output string.
}
return sOut
}
Anyway, I'm also looking for code to do that but for delimited lists and enumerating backwards through array/objects, etc. But for those all I can think of is creating another array and writing all the keys in reverse order then using that to enumerate.
Edit: Here's a function I came up with for reversing a delimited list, if anybody can come up with a better/faster function let me know.
Code: Select all
reverse_list(ByRef p_In, p_Delim=" ", p_Case=1) {
VarSetCapacity(sOut, VarSetCapacity(p_In))
i2 := StrLen(p_In) + 1
dLen := StrLen(p_Delim)
while (i1:=Instr(p_In, p_Delim, p_Case, 0, A_Index))>0
{ sOut .= ((A_Index > 1)?p_Delim:"") . SubStr(p_In, i1+dLen, (i2-i1)-1)
i2 := i1
}
sOut .= ((sOut = "")?"":p_Delim) . SubStr(p_In, 1, i2-1)
return sOut
}
Although I'm still looking for the best way to enumerate backwards through an object. The only thing I can think of would be to enumerate forwards, add the keys to another array in reverse order, then enumerate that for the keys.