Ran
02/02/2021, 8:46 AMstringByAddingPercentEncodingWithAllowedCharacters
doesn’t work on Kotlin Native. My code is like following:
actual fun urlEncode(url: String) : String {
val set = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as NSMutableCharacterSet
set.addCharactersInString("+&")
return NSString.create(string=url).stringByAddingPercentEncodingWithAllowedCharacters(
allowedCharacters = NSCharacterSet.URLQueryAllowedCharacterSet()
) ?: ""
}
My test code:
@Test
fun testUrlEncode() {
assertEquals(
"https%3A%2F%2Fmontageapps.com%3Ftoken%3Drth%26price%3Dinvaluable",
urlEncode("<https://montageapps.com?token=rth&price=invaluable>")
)
}
Test result:
kotlin.AssertionError: Expected <https%3A%2F%<http://2Fmontageapps.com|2Fmontageapps.com>%3Ftoken%3Drth%26price%3Dinvaluable>, actual <<https://montageapps.com?token=rth&price=invaluable>>.
Artyom Degtyarev [JB]
02/02/2021, 9:59 AMfun urlEncode(url: String) : String {
val set = NSCharacterSet.letterCharacterSet().mutableCopy() as NSMutableCharacterSet
set.addCharactersInString(".")
return NSString.create(string=url).stringByAddingPercentEncodingWithAllowedCharacters(
allowedCharacters = set
) ?: ""
}
Ran
02/02/2021, 10:00 AMNSCharacterSet.URLQueryAllowedCharacterSet
doesn’t workArtyom Degtyarev [JB]
02/02/2021, 10:18 AM