OWL-Varun commented on code in PR #799:
URL: https://github.com/apache/daffodil/pull/799#discussion_r888988158
##########
daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLRunner.scala:
##########
@@ -2362,32 +2359,32 @@ sealed abstract class DocumentPart(part: Node, parent:
Document) {
}.trim.toUpperCase()
/*
- * Description: Compare ByteDocumentPart/BitsDocumentPart string to a string
of valid characters.
- * If the string runs into non-valid characters, skip over them.
- * If the string runs into "//", any character following it will
be skipped (including valid ones).
- * Characters on a new line will be considered again, unless
they are found after a "//"
- * Parameters: String containing the acceptable characters (0,1 for bits)
(0-9,A-F,a-f for bytes)
- * Return: ListBuffer[Char] of the accepted characters from the
ByteDocumentPart/BitsDocumentPart element
+ * Allow "//" and "/* */" (inline) to act as comments.
+ * Any characters not explicitly allowed are also considered comments and are
removed.
*/
- def commentCompatibleFormat(validCharacters: String) : ListBuffer[Char] = {
- val tempListBuffer = new ListBuffer[Char]()
- var noCommentBool = true
-
- for (i <- 0 to partRawContent.length()-1){
- //Check if valid
- if(noCommentBool && validCharacters.contains(partRawContent(i))){
- tempListBuffer += partRawContent(i)
- }
- else if(partRawContent(i) == '/' && i < partRawContent.length()-1){
- if(partRawContent(i+1) == '/'){
- noCommentBool = false
- }
- }
- else if(partRawContent(i) == '\n'){
- noCommentBool = true
- }
+ def removeComments(validCharactersSet : String, userData : String ) : String
= {
+ lazy val doubleForwardPattern = "//.*".r
+ lazy val openClosePattern = "/[*].*[*]/".r
+
+ //Remove the comments (//) and (/* */)
+ lazy val noCommentsForward = doubleForwardPattern.replaceAllIn(userData,"")
+ lazy val noCommentsBothFormats =
openClosePattern.replaceAllIn(noCommentsForward,"")
+
+ //Throw exception if /* or */ still found. This means user input was not
formatted correctly.
+ if(noCommentsBothFormats.contains("/*") ||
noCommentsBothFormats.contains("*/")){
+ throw TDMLException("Improper formatting of /* */ style comment", None)
}
- tempListBuffer
+
+ //Remove the new lines - concatenate after
+ lazy val noCommentsString = noCommentsBothFormats.split('\n').mkString
+
+ //Iterate over and check value of the characters, if invalid character
found create log and skip over it
Review Comment:
What are your thoughts on adding " " and "." to the noWarnChars variable, as
well? Is that being too lenient or have you seen enough instances where " " and
"." are used that we should accept not warning the user?
##########
daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLRunner.scala:
##########
@@ -2362,32 +2359,32 @@ sealed abstract class DocumentPart(part: Node, parent:
Document) {
}.trim.toUpperCase()
/*
- * Description: Compare ByteDocumentPart/BitsDocumentPart string to a string
of valid characters.
- * If the string runs into non-valid characters, skip over them.
- * If the string runs into "//", any character following it will
be skipped (including valid ones).
- * Characters on a new line will be considered again, unless
they are found after a "//"
- * Parameters: String containing the acceptable characters (0,1 for bits)
(0-9,A-F,a-f for bytes)
- * Return: ListBuffer[Char] of the accepted characters from the
ByteDocumentPart/BitsDocumentPart element
+ * Allow "//" and "/* */" (inline) to act as comments.
+ * Any characters not explicitly allowed are also considered comments and are
removed.
*/
- def commentCompatibleFormat(validCharacters: String) : ListBuffer[Char] = {
- val tempListBuffer = new ListBuffer[Char]()
- var noCommentBool = true
-
- for (i <- 0 to partRawContent.length()-1){
- //Check if valid
- if(noCommentBool && validCharacters.contains(partRawContent(i))){
- tempListBuffer += partRawContent(i)
- }
- else if(partRawContent(i) == '/' && i < partRawContent.length()-1){
- if(partRawContent(i+1) == '/'){
- noCommentBool = false
- }
- }
- else if(partRawContent(i) == '\n'){
- noCommentBool = true
- }
+ def removeComments(validCharactersSet : String, userData : String ) : String
= {
+ lazy val doubleForwardPattern = "//.*".r
+ lazy val openClosePattern = "/[*].*[*]/".r
+
+ //Remove the comments (//) and (/* */)
+ lazy val noCommentsForward = doubleForwardPattern.replaceAllIn(userData,"")
+ lazy val noCommentsBothFormats =
openClosePattern.replaceAllIn(noCommentsForward,"")
+
+ //Throw exception if /* or */ still found. This means user input was not
formatted correctly.
+ if(noCommentsBothFormats.contains("/*") ||
noCommentsBothFormats.contains("*/")){
+ throw TDMLException("Improper formatting of /* */ style comment", None)
}
- tempListBuffer
+
+ //Remove the new lines - concatenate after
+ lazy val noCommentsString = noCommentsBothFormats.split('\n').mkString
+
+ //Iterate over and check value of the characters, if invalid character
found create log and skip over it
Review Comment:
What are your thoughts on adding " " and "." to the noWarnChars variable, as
well? Is that being too lenient or have you seen enough instances where " " and
"." are used that we should accept not warning the user?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]