From f56d1c8d6c9ebdbee0bbf3e733e9defe43746dcf Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 29 May 2024 02:43:29 +0200 Subject: [PATCH 1/3] Fix parsing numbers Use java.lang.Integer.decode which handles other radices besides 10 Also remove `_` from the number string --- .../scala/org/virtuslab/yaml/YamlDecoder.scala | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala b/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala index fe0c10523..a004055b3 100644 --- a/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala +++ b/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala @@ -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 - .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) => From 5f555b7306a15ddac275df76cb19dea18b3e9197 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 29 May 2024 12:15:24 +0200 Subject: [PATCH 2/3] Fix tests --- .../main/scala/org/virtuslab/yaml/YamlDecoder.scala | 10 +++++++--- .../org/virtuslab/yaml/decoder/DecoderSuite.scala | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala b/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala index a004055b3..d15d72119 100644 --- a/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala +++ b/core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala @@ -77,14 +77,18 @@ object YamlDecoder extends YamlDecoderCompanionCrossCompat { val valueNorm = value.replaceAll("_", "") Try(java.lang.Integer.decode(valueNorm)) .orElse(Try(java.lang.Long.decode(valueNorm))) + .orElse(Try(BigInt(valueNorm))) .toEither .left .map(t => ConstructError.from(t, "int", node)) case Tag.float => val valueNorm = value.replaceAll("_", "") - valueNorm.toFloatOption - .orElse(valueNorm.toDoubleOption) - .toRight(cannotParse(value, "float", node)) + Try(java.lang.Float.parseFloat(valueNorm)) + .orElse(Try(java.lang.Double.parseDouble(valueNorm))) + .orElse(Try(BigDecimal(valueNorm))) + .toEither + .left + .map(t => ConstructError.from(t, "float", node)) case Tag.str => Right(value) } case MappingNode(mappings, Tag.map) => diff --git a/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala b/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala index f57c2d8bf..3a1de9a3d 100644 --- a/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala +++ b/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala @@ -307,7 +307,7 @@ class DecoderSuite extends munit.FunSuite: 123 -> 321, "string" -> "aezakmi", true -> false, - 5.5 -> 55.55 + 5.5f -> 55.55f ) assertEquals(yaml.as[Map[Any, Any]], Right(expected)) From 81901ac22de0ac947c0fbf143d2684b9ff00be50 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 29 May 2024 12:25:19 +0200 Subject: [PATCH 3/3] fmt --- .../test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala b/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala index 3a1de9a3d..d18ac38aa 100644 --- a/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala +++ b/core/shared/src/test/scala-3/org/virtuslab/yaml/decoder/DecoderSuite.scala @@ -307,7 +307,7 @@ class DecoderSuite extends munit.FunSuite: 123 -> 321, "string" -> "aezakmi", true -> false, - 5.5f -> 55.55f + 5.5f -> 55.55f ) assertEquals(yaml.as[Map[Any, Any]], Right(expected))