More optimization to spaceToPlus

This commit is contained in:
Erik C. Thauvin 2023-01-06 10:33:49 -08:00
parent f8b9376f40
commit 7c8a4358b6
3 changed files with 23 additions and 24 deletions

View file

@ -1,6 +1,6 @@
<component name="CopyrightManager"> <component name="CopyrightManager">
<copyright> <copyright>
<option name="notice" value="Copyright 2001-&amp;#36;today.year Geert Bevin (gbevin[remove] at uwyn dot com)&#10;Copyright &amp;#36;today.year Erik C. Thauvin (erik@thauvin.net)&#10;&#10;Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);&#10;you may not use this file except in compliance with the License.&#10;You may obtain a copy of the License at&#10;&#10; http://www.apache.org/licenses/LICENSE-2.0&#10;&#10;Unless required by applicable law or agreed to in writing, software&#10;distributed under the License is distributed on an &quot;AS IS&quot; BASIS,&#10;WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&#10;See the License for the specific language governing permissions and&#10;limitations under the License." /> <option name="notice" value="Copyright 2001-&amp;#36;today.year Geert Bevin (gbevin[remove] at uwyn dot com)&#10;Copyright 2022-&amp;#36;today.year Erik C. Thauvin (erik@thauvin.net)&#10;&#10;Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);&#10;you may not use this file except in compliance with the License.&#10;You may obtain a copy of the License at&#10;&#10; http://www.apache.org/licenses/LICENSE-2.0&#10;&#10;Unless required by applicable law or agreed to in writing, software&#10;distributed under the License is distributed on an &quot;AS IS&quot; BASIS,&#10;WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&#10;See the License for the specific language governing permissions and&#10;limitations under the License." />
<option name="myName" value="Apache License" /> <option name="myName" value="Apache License" />
</copyright> </copyright>
</component> </component>

View file

@ -1,6 +1,6 @@
/* /*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com) * Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Copyright 2023 Erik C. Thauvin (erik@thauvin.net) * Copyright 2022-2023 Erik C. Thauvin (erik@thauvin.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ import kotlin.system.exitProcess
object UrlEncoder { object UrlEncoder {
private val hexDigits = "0123456789ABCDEF".toCharArray() private val hexDigits = "0123456789ABCDEF".toCharArray()
internal val usage = internal val usage =
"Usage : java -jar urlencoder-*all.jar [-ed] <text>" + System.lineSeparator() + "Usage : java -jar urlencoder-*all.jar [-ed] text" + System.lineSeparator() +
"Encode and decode URL components defensively." + System.lineSeparator() + " -e encode (default) " + "Encode and decode URL components defensively." + System.lineSeparator() + " -e encode (default) " +
System.lineSeparator() + " -d decode" System.lineSeparator() + " -d decode"
@ -160,13 +160,13 @@ object UrlEncoder {
out = StringBuilder(source.length) out = StringBuilder(source.length)
out.append(source, 0, i) out.append(source, 0, i)
} }
if (spaceToPlus && ch == ' ') {
out.append('+')
i++
} else {
val cp = source.codePointAt(i) val cp = source.codePointAt(i)
if (cp < 0x80) { if (cp < 0x80) {
if (spaceToPlus && ch == ' ') {
out.append('+')
} else {
out.appendEncodedByte(cp) out.appendEncodedByte(cp)
}
i++ i++
} else if (Character.isBmpCodePoint(cp)) { } else if (Character.isBmpCodePoint(cp)) {
for (b in ch.toString().toByteArray(StandardCharsets.UTF_8)) { for (b in ch.toString().toByteArray(StandardCharsets.UTF_8)) {
@ -183,7 +183,6 @@ object UrlEncoder {
} }
} }
} }
}
return out?.toString() ?: source return out?.toString() ?: source
} }
@ -191,7 +190,7 @@ object UrlEncoder {
/** /**
* Encodes and decodes URLs from the command line. * Encodes and decodes URLs from the command line.
* *
* - `java -jar urlencoder-*all.jar <text>` * - `java -jar urlencoder-*all.jar [-ed] text`
*/ */
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {

View file

@ -1,6 +1,6 @@
/* /*
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com) * Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
* Copyright 2023 Erik C. Thauvin (erik@thauvin.net) * Copyright 2022-2023 Erik C. Thauvin (erik@thauvin.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.