Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing numbers #299

Merged
merged 3 commits into from
May 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ object YamlDecoder extends YamlDecoderCompanionCrossCompat {
case Tag.nullTag => Right(None)
case Tag.boolean => value.toBooleanOption.toRight(cannotParse(value, "Boolean", node))
case Tag.int =>
if (value.startsWith("0b"))
Try(Integer.parseInt(value.drop(2), 8)).toEither.left
.map(t => ConstructError.from(t, "Int", node))
else if (value.startsWith("0x"))
Try(Integer.parseInt(value.drop(2), 8)).toEither.left
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 instead of 16.

But it's better to leave it to Long.decode anyway.

.map(t => ConstructError.from(t, "Int", node))
else value.toIntOption.toRight(cannotParse(value, "Int", node))
val valueNorm = value.replaceAll("_", "")
Try(java.lang.Integer.decode(valueNorm))
.orElse(Try(java.lang.Long.decode(valueNorm)))
.toEither
.left
.map(t => ConstructError.from(t, "int", node))
case Tag.float =>
value.toDoubleOption.toRight(cannotParse(value, "Double", node))
val valueNorm = value.replaceAll("_", "")
valueNorm.toFloatOption
.orElse(valueNorm.toDoubleOption)
.toRight(cannotParse(value, "float", node))
case Tag.str => Right(value)
}
case MappingNode(mappings, Tag.map) =>
Expand Down
Loading