Transform, OutputVar, URLEncode

Propose new features and changes

Should this be implemented

Yes, as stated in the post
8
62%
Yes, but in a different command/function
0
No votes
No, this should be left out
4
31%
Other, state why in a reply
1
8%
 
Total votes: 13
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Transform, OutputVar, URLEncode

27 Feb 2014, 17:00

I've been poking around for a good, simple, fully funcitonal pair of Uri/Url/Percent encode and decode functions, and there doesn't seem to be an elegant solution. Some people say to use monstrosities of code, some people say to use javascript COM objects, and some people say to use simple solutions that don't actually work as needed.

It seems trivial to do this in other languages, so why not AHK? I'd imagine it would be placed into the Transform command, similarly to the HTML escape feature. Perhaps as "URLEncode"/"URLDecode" or "PercentEncode"/"PercentDecode".

Your thoughts on the matter?
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Transform, OutputVar, URLEncode

01 Mar 2014, 17:35

Is it trivial in "other languages" which are not aimed at web development? I can see it is sometimes useful in AutoHotkey, but not necessarily so useful as to warrant built-in functionality. All you need is a well-written script function. A built-in version would not necessarily work better.

The Transform command has been mostly superseded and has been removed from v2 (Deref has been promoted to a separate command). If any URL encoding/decoding functionality is added, it will be in the form of built-in functions.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Transform, OutputVar, URLEncode

11 Mar 2014, 12:20

Close to half the lanuages on the rosettacode page for url encoding either have a built in function for it, or a standard library for it. That includes a few variations of BASIC, as well as C#, D, F#, Go, Java, etc.

If you don't want to implement a url encode function, perhaps you would consider a method of converting to a utf-8 bytecode instead of varsetcapacity, strput, and numget? It's kind of clunky, and confusing. I never would have thought of it if it weren't for extensive forum searching.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Transform, OutputVar, URLEncode

11 Mar 2014, 17:02

Okay, you've got me there. However, consider the size of those libraries compared to the size of AutoHotkey. Some of them have everything and the kitchen sink, so the fact they include URL encoding isn't saying much.

For simpler string encoding, you can copy-paste StrPutVar from the StrPut/StrGet page (or save it into your library). Since string conversion is rarely needed, I'd rather have one or two powerful built-in functions than a simple one for each task. (Rarely needed aside from the cases that are already covered: FileAppend, FileRead and FileOpen have ways of specifying the encoding, and DllCall has AStr/WStr.)

You only need NumGet to get each character because UTF-8 strings aren't readable on Unicode versions of AutoHotkey. The same would be true even if there was a simpler way to convert to UTF-8. That is, unless you're asking for a function to convert to "pseudo-UTF-8" (i.e. not UTF-8, but a similar encoding which stretches each byte out to 16 bits). That I just won't do. (Anyway, once you have pseudo-UTF-8, you'd still need to extract the code of each character, or if you're doing one character at a time, each byte/code unit.)

Again, all you need is a well-written script function. Once you have the function, you don't need to write it again or even know how it works.
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Transform, OutputVar, URLEncode

15 Mar 2014, 15:01

"Other, state why in a reply"
Yes, I think a function for this is a good idea, but... All these languages are ... (if you don't mind me saying) .. "bulky/bloated" ... A library/function in "Lib\" would do fine in this case. no need to increase "bulky"-ness for everybody where only a few would use it... :P ... I am not necessarily disagreeing with you but.. autohotkey was not even supposed to be a "full-blown" language other then for helping simple everyday users ...

regards
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Transform, OutputVar, URLEncode

16 Dec 2016, 17:04

Well then do we have a good standard function for this?
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Transform, OutputVar, URLEncode

17 Dec 2016, 03:03

How would that handle Ä Ü and Ö?
Recommends AHK Studio
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Transform, OutputVar, URLEncode

18 Dec 2016, 16:11

try it :)
%C3%84%20%C3%9C%20and%20%C3%96

Code: Select all

msgbox % LC_UriEncode("Ä Ü and Ö")
LC_UriEncode(Uri, RE="[0-9A-Za-z]") {
	VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0), StrPut(Uri, &Var, "UTF-8")
	While Code := NumGet(Var, A_Index - 1, "UChar")
		Res .= (Chr:=Chr(Code)) ~= RE ? Chr : Format("%{:02X}", Code)
	Return, Res
}
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
guestuser

Re: Transform, OutputVar, URLEncode

19 Dec 2016, 01:42

Hi, these are for UTF-8 encode/decode only, tested on 32/64bit.

Code: Select all

URI_Encode(Str, All := False)
{
    Static doc := ComObjCreate("HTMLfile")
    Try
    {
        doc.write("<body><script>document.body.innerText = encodeURI" . (All ? "Component" : "") . "(""" . Str . """);</script>")
        Return, doc.body.innerText, doc.body.innerText := ""
    }
}

URI_Decode(Str)
{
    Static doc := ComObjCreate("HTMLfile")
    Try
    {
        doc.write("<body><script>document.body.innerText = decodeURIComponent(""" . Str . """);</script>")
        Return, doc.body.innerText, doc.body.innerText := ""
    }
}
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Transform, OutputVar, URLEncode

19 Dec 2016, 06:55

That can be a bit heavy depending on what you're doing. :0
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 25 guests