stevedlawrence commented on a change in pull request #75: Daffodil 1738 zoned 
decimal
URL: https://github.com/apache/incubator-daffodil/pull/75#discussion_r195405339
 
 

 ##########
 File path: 
daffodil-lib/src/main/scala/org/apache/daffodil/util/DecimalUtils.scala
 ##########
 @@ -335,4 +335,142 @@ object DecimalUtils {
     outArray
   }
 
+  def convertFromAsciiStandard(digit: Int): (Int, Boolean) = {
+    if ((digit >= 48) && (digit <= 57)) // positive 0-9
+      return (digit - 48, false)
+    else if ((digit >= 112) && (digit <= 121)) // negative 0-9
+      return (digit - 112, true)
+    else
+      throw new NumberFormatException("Invalid zoned digit: " + digit)
+  }
+
+  def convertToAsciiStandard(digit: Char, positive: Boolean, op: Boolean): 
Char = {
+    if (positive || !op)
+      return digit
+    else
+      return (digit + 64).asInstanceOf[Char]
+  }
+
+  def convertFromAsciiTranslatedEBCDIC(digit: Int): (Int, Boolean) = {
+    if (digit == 123)
+      return (0, false)
+    else if (digit == 125)
+      return (0, true)
+    else if ((digit >= 65) && (digit <= 73)) // positive 1-9
+      return (digit - 64, false)
+    else if ((digit >= 74) && (digit <= 82)) // negative 1-9
+      return (digit - 73, true)
+    else if ((digit >= 48) && (digit <= 57))
+      return (digit - 48, false) // non-overpunched digit
+    else
+      throw new NumberFormatException("Invalid zoned digit: " + digit)
+  }
+
+  def convertToAsciiTranslatedEBCDIC(digit: Char, positive: Boolean, op: 
Boolean): Char = {
+    val result = (positive, op) match {
+      case (true, true) => { // Overpunch character with positive sign
+        if (digit == '0')
+          123
+        else
+          digit + 16
+      }
+      case (false, true) => { // Overpunch character with negative sign
+        if (digit == '0')
+          125
+        else
+          digit + 25
+      }
+      case _ => digit
+    }
+
+    return result.asInstanceOf[Char]
+  }
+
+  def convertFromAsciiCARealiaModified(digit: Int): (Int, Boolean) = {
+    if ((digit >= 48) && (digit <= 57)) // positive 0-9
+      return (digit - 48, false)
+    else if ((digit >= 32) && (digit <= 41)) // negative 0-9
+      return (digit - 32, true)
+    else
+      throw new NumberFormatException("Invalid zoned digit: " + digit)
+  }
+
+  def convertToAsciiCARealiaModified(digit: Char, positive: Boolean, op: 
Boolean): Char = {
+    if (positive || !op)
+      return digit
+    else
+      return (digit - 16).asInstanceOf[Char]
+  }
+
+  def convertFromAsciiTandemModified(digit: Int): (Int, Boolean) = {
+    if ((digit >= 48) && (digit <= 57)) // positive 0-9
+      return (digit - 48, false)
+    else if ((digit >= 128) && (digit <= 137)) // negative 0-9
+      return (digit - 128, true)
+    else
+      throw new NumberFormatException("Invalid zoned digit: " + digit)
+  }
+
+  def convertToAsciiTandemModified(digit: Char, positive: Boolean, op: 
Boolean): Char = {
+    if (positive || !op)
+      return digit
+    else
+      return (digit + 80).asInstanceOf[Char]
+  }
+
+  def zonedToNumber(num: String, zonedStyle: TextZonedSignStyle): String = {
+    val decodedValue = new StringBuilder()
+    var negative = false
+
+    for (char <- num.chars().toArray) {
+      val (digit, opneg) = zonedStyle match {
+        case TextZonedSignStyle.AsciiStandard => convertFromAsciiStandard(char)
+        case TextZonedSignStyle.AsciiTranslatedEBCDIC => 
convertFromAsciiTranslatedEBCDIC(char)
+        case TextZonedSignStyle.AsciiCARealiaModified => 
convertFromAsciiCARealiaModified(char)
+        case TextZonedSignStyle.AsciiTandemModified => 
convertFromAsciiTandemModified(char)
+      }
+
+      if (opneg && !negative) {
+        negative = true
+        decodedValue.insert(0, '-')
+      }
 
 Review comment:
   If data is malformed, is it possible that there could be multiple characters 
that look like they are overpunched? What happens in that case? Looks like it 
would not reinsert a negative sign, but continues on. Should that throw a 
NumberFormatException?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to