jadams-tresys commented on a change in pull request #75: Daffodil 1738 zoned
decimal
URL: https://github.com/apache/incubator-daffodil/pull/75#discussion_r195429174
##########
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:
I think this was a carry over from when I didn't have a full understanding
of the concept of overpunching the sign. I assumed that in negative numbers
all the digits were overpunched with the negative sign, not just one. Will fix.
----------------------------------------------------------------
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