stevedlawrence commented on a change in pull request #16: Implemented packed binary formats URL: https://github.com/apache/incubator-daffodil/pull/16#discussion_r163042307
########## File path: daffodil-lib/src/main/scala/edu/illinois/ncsa/daffodil/util/DecimalUtils.scala ########## @@ -0,0 +1,328 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package edu.illinois.ncsa.daffodil.util + +import edu.illinois.ncsa.daffodil.exceptions.Assert +import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.BinaryNumberCheckPolicy + +import java.math.{ BigInteger => JBigInteger, BigDecimal => JBigDecimal } + +object PackedSignCodes { + def apply(signCodes: String, policy: BinaryNumberCheckPolicy) = { + val str = signCodes.replaceAll("\\s", "") + val chars = str.toCharArray() + Assert.invariant(chars.length == 4) + + val positive = policy match { + case BinaryNumberCheckPolicy.Strict => List(Integer.parseInt(String.valueOf(chars(0)), 16)) + case BinaryNumberCheckPolicy.Lax => List(Integer.parseInt(String.valueOf(chars(0)), 16), 0xA, 0xC, 0xE, 0xF) Review comment: This might be a little easier to read if do some math to conver the char to an int. Given an char in the range of 'A' to 'F', you can subtract 55 and that will convert it to the integer hex version. So this could just be ``` BinaryNumberCheckPolicy.Strict => List(chars(0) - 55) ``` That isn't as obvious, so it might be worth adding a comment about what the - 55 is doing. Also, when in Lax mode, the first item will be a duplicate of something else in the list. It might be a good idea to call .distinct on then new list to remove the duplicate. Might be helpful if we ever print out the valid sign codes to not have a duplicate. ---------------------------------------------------------------- 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
