Is there an EncodeURLComponent equivalent in iOS or the new framework yet?
At first glance, it does not seem out of reach of a series of replaceAll : https://en.wikipedia.org/wiki/Percent-encoding
Unless of course there is something in the framework that does it so a declare can be constructed.
No
I found this somewhere, probably on this forum, but I can’t remember where or from whom so I apologise that I can’t acknowledge the author. I use it when I post binaries to our web services to ensure the filename in the request is properly encapsulated:
Function EncodeURLComponent(value as text) As text
Declare Function CFURLCreateStringByAddingPercentEscapes lib "Foundation" (allocator as Ptr, origString as CFStringRef , charactersToLeaveUnescaped as CFStringRef , legalURLCharactersToBeEscaped as cfStringRef,encoding as uint32) as CFStringRef
return CFURLCreateStringByAddingPercentEscapes(nil, value, nil, nil, &h08000100)
End Function
We really need to start a wiki that show Old Framework and New Framework equivalents.
I wrote one using the new framework
Its a port of some C code
They could be wildly wrong
It was just an exercise to see how difficult it would be
Protected Function urlencode(s as text) As text
dim buf as text
For Each codePoint As UInt32 In s.Codepoints
If codePoint >= 48 and codepoint <= 57 Then // 0 - 9
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codePoint >= 65 and codepoint <= 90 Then // A - Z
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codePoint >= 97 and codepoint <= 122 Then // a - z
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codepoint = 45 then // "-"
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codepoint = 95 then // "_"
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codepoint = 46 then // "."
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codepoint = 126 then // "~"
buf = buf + Text.FromUnicodeCodepoint(codePoint)
elseif codepoint = 32 then // " "
buf = buf + "+" // may also be encoded as %20
else
buf = buf + "%" + hex(codepoint)
end if
next
return buf
End Function
Protected Function hex(codepoint as uint32) As text
// takes the codepoint and returns it as a string of hex chars
Dim t As Text = Text.FromUnicodeCodepoint( codePoint )
Dim data As Xojo.Core.MemoryBlock
data = Xojo.Core.TextEncoding.ASCII.ConvertTextToData(t)
static hexchars as text = "0123456789ABCDEF"
dim buf as text
for i as integer = 0 to data.Size - 1
dim hinibble as uint8 = data.Int8Value(i)
hinibble = (hinibble and &hF0) \\ 16
dim lownibble as uint8 = data.Int8Value(i)
lownibble = (lownibble and &h0F)
buf = buf + hexchars.Mid(hinibble,1) + hexchars.Mid(lownibble,1)
next
return buf
End Function
I would donate my domain xojo.io to that cause. I never had enough free time to get the file sharing thing off the ground.
[quote=270929:@Norman Palardy]I wrote one using the new framework
Its a port of some C code[/quote]
Some minor abuse of a Select statement and the To keyword saves a lot of typing.
Protected Function urlencode(s as text) As text
dim buf as text
For Each codePoint As UInt32 In s.Codepoints
Select Case codePoint
Case 48 To 57, 65 To 90, 97 To 122, 45, 46, 95
buf = buf + Text.FromUnicodeCodepoint(codePoint)
Else
buf = buf + "%" + hex(codepoint)
End Select
Next
return buf
End Function
Just wanted clarity with the added comments what it was doing
The line
buf = buf + Text.FromUnicodeCodepoint(codePoint)
was cut & paste so no extra typing except for a handful of cmd-v / ctrl-v’s