-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
zio-http/js/src/main/scala/zio/http/codec/PathCodecPlatformSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package zio.http.codec | ||
|
||
import java.util.Objects | ||
|
||
trait PathCodecPlatformSpecific { | ||
private[codec] def parseLong(s: CharSequence, beginIndex: Int, endIndex: Int, radix: Int): Long = { | ||
Objects.requireNonNull(s) | ||
Objects.checkFromToIndex(beginIndex, endIndex, s.length) | ||
if (radix < Character.MIN_RADIX) | ||
throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX") | ||
if (radix > Character.MAX_RADIX) | ||
throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX") | ||
var negative = false | ||
var i = beginIndex | ||
var limit = -Long.MaxValue | ||
if (i < endIndex) { | ||
val firstChar = s.charAt(i) | ||
if (firstChar < '0') { // Possible leading "+" or "-" | ||
if (firstChar == '-') { | ||
negative = true | ||
limit = Long.MinValue | ||
} else if (firstChar != '+') throw forCharSequence(s, beginIndex, endIndex, i) | ||
i += 1 | ||
} | ||
if (i >= endIndex) { // Cannot have lone "+", "-" or "" | ||
throw forCharSequence(s, beginIndex, endIndex, i) | ||
} | ||
val multmin = limit / radix | ||
var result = 0L | ||
while (i < endIndex) { | ||
// Accumulating negatively avoids surprises near MAX_VALUE | ||
val digit = Character.digit(s.charAt(i), radix) | ||
if (digit < 0 || result < multmin) throw forCharSequence(s, beginIndex, endIndex, i) | ||
result *= radix | ||
if (result < limit + digit) throw forCharSequence(s, beginIndex, endIndex, i) | ||
i += 1 | ||
result -= digit | ||
} | ||
if (negative) result | ||
else -result | ||
} else throw new NumberFormatException("") | ||
} | ||
|
||
private[codec] def parseInt(s: CharSequence, beginIndex: Int, endIndex: Int, radix: Int): Int = { | ||
Objects.requireNonNull(s) | ||
Objects.checkFromToIndex(beginIndex, endIndex, s.length) | ||
if (radix < Character.MIN_RADIX) | ||
throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX") | ||
if (radix > Character.MAX_RADIX) | ||
throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX") | ||
var negative = false | ||
var i = beginIndex | ||
var limit = -Int.MaxValue | ||
if (i < endIndex) { | ||
val firstChar = s.charAt(i) | ||
if (firstChar < '0') { // Possible leading "+" or "-" | ||
if (firstChar == '-') { | ||
negative = true | ||
limit = Int.MinValue | ||
} else if (firstChar != '+') throw forCharSequence(s, beginIndex, endIndex, i) | ||
i += 1 | ||
if (i == endIndex) { // Cannot have lone "+" or "-" | ||
throw forCharSequence(s, beginIndex, endIndex, i) | ||
} | ||
} | ||
val multmin = limit / radix | ||
var result = 0 | ||
while (i < endIndex) { | ||
// Accumulating negatively avoids surprises near MAX_VALUE | ||
val digit = Character.digit(s.charAt(i), radix) | ||
if (digit < 0 || result < multmin) throw forCharSequence(s, beginIndex, endIndex, i) | ||
result *= radix | ||
if (result < limit + digit) throw forCharSequence(s, beginIndex, endIndex, i) | ||
i += 1 | ||
result -= digit | ||
} | ||
if (negative) result | ||
else -result | ||
} else throw forInputString("", radix) | ||
} | ||
|
||
private[codec] def forCharSequence(s: CharSequence, beginIndex: Int, endIndex: Int, errorIndex: Int) = | ||
new NumberFormatException( | ||
"Error at index " + (errorIndex - beginIndex) + " in: \"" + s.subSequence(beginIndex, endIndex) + "\"", | ||
) | ||
|
||
private[codec] def forInputString(s: String, radix: Int) = new NumberFormatException( | ||
"For input string: \"" + s + "\"" + (if (radix == 10) "" | ||
else " under radix " + radix), | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
zio-http/jvm/src/main/scala/zio/http/codec/PathCodecPlatformSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package zio.http.codec | ||
|
||
trait PathCodecPlatformSpecific { | ||
def parseLong(s: CharSequence, beginIndex: Int, endIndex: Int, radix: Int): Long = | ||
java.lang.Long.parseLong(s.subSequence(beginIndex, endIndex).toString, radix) | ||
|
||
def parseInt(s: CharSequence, beginIndex: Int, endIndex: Int, radix: Int): Int = | ||
java.lang.Integer.parseInt(s.subSequence(beginIndex, endIndex).toString, radix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters