Collecting now all chars that should be added to part in a StringBuilder which seems to be the fastest solution
This commit is contained in:
parent
40c5d64103
commit
b93ce1ca0d
|
@ -43,7 +43,7 @@ open class ResponseParser @JvmOverloads constructor(
|
||||||
|
|
||||||
open fun parse(response: String): Response {
|
open fun parse(response: String): Response {
|
||||||
try {
|
try {
|
||||||
val segments = splitIntoPartsAndUnmask(response, Separators.SegmentSeparator).toMutableList()
|
val segments = splitIntoPartsAndUnmask(response, Separators.SegmentSeparatorChar).toMutableList()
|
||||||
|
|
||||||
extractSegmentEmbeddedInEncryptedData(segments)
|
extractSegmentEmbeddedInEncryptedData(segments)
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ open class ResponseParser @JvmOverloads constructor(
|
||||||
protected open fun parseSegment(segment: String): ReceivedSegment? {
|
protected open fun parseSegment(segment: String): ReceivedSegment? {
|
||||||
try {
|
try {
|
||||||
if (segment.isNotEmpty()) { // filter out empty lines
|
if (segment.isNotEmpty()) { // filter out empty lines
|
||||||
val dataElementGroups = splitIntoPartsAndUnmaskCharByChar(segment, Separators.DataElementGroupsSeparatorChar)
|
val dataElementGroups = splitIntoPartsAndUnmask(segment, Separators.DataElementGroupsSeparatorChar)
|
||||||
|
|
||||||
val segmentId = segment.substring(0, segment.indexOf(Separators.DataElementsSeparator))
|
val segmentId = segment.substring(0, segment.indexOf(Separators.DataElementsSeparator))
|
||||||
|
|
||||||
|
@ -715,7 +715,7 @@ open class ResponseParser @JvmOverloads constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun getDataElements(dataElementGroup: String): List<String> {
|
protected open fun getDataElements(dataElementGroup: String): List<String> {
|
||||||
return splitIntoPartsAndUnmaskCharByChar(dataElementGroup, Separators.DataElementsSeparatorChar)
|
return splitIntoPartsAndUnmask(dataElementGroup, Separators.DataElementsSeparatorChar)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -728,69 +728,43 @@ open class ResponseParser @JvmOverloads constructor(
|
||||||
*
|
*
|
||||||
* Also binary data shouldn't be taken into account.
|
* Also binary data shouldn't be taken into account.
|
||||||
*/
|
*/
|
||||||
protected open fun splitIntoPartsAndUnmask(dataString: String, separator: String): List<String> {
|
protected open fun splitIntoPartsAndUnmask(dataString: String, separator: Char): List<String> {
|
||||||
val separatorIndices = messageUtils.findSeparatorIndices(dataString, separator)
|
|
||||||
|
|
||||||
var startIndex = 0
|
|
||||||
val elements = separatorIndices.map { endIndex ->
|
|
||||||
val element = dataString.substring(startIndex, endIndex)
|
|
||||||
startIndex = endIndex + 1
|
|
||||||
element
|
|
||||||
}.toMutableList()
|
|
||||||
|
|
||||||
if (startIndex < dataString.length) {
|
|
||||||
elements.add(dataString.substring(startIndex))
|
|
||||||
}
|
|
||||||
|
|
||||||
val maskedSeparator = Separators.MaskingCharacter + separator
|
|
||||||
|
|
||||||
return elements.map {
|
|
||||||
if (it.contains(maskedSeparator)) it.replace(maskedSeparator, separator)
|
|
||||||
else it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected open fun splitIntoPartsAndUnmaskCharByChar(dataString: String, separator: Char): List<String> {
|
|
||||||
val binaryRanges = messageUtils.findBinaryDataRanges(dataString)
|
val binaryRanges = messageUtils.findBinaryDataRanges(dataString)
|
||||||
|
|
||||||
var lastEndIndex = 0
|
|
||||||
var partContainsMaskedSeparators = false
|
|
||||||
val parts = mutableListOf<String>()
|
val parts = mutableListOf<String>()
|
||||||
|
var part = StringBuilder()
|
||||||
|
|
||||||
for (i in dataString.indices) {
|
for (i in dataString.indices) {
|
||||||
val char = dataString[i]
|
val char = dataString[i]
|
||||||
|
|
||||||
if (char == separator && messageUtils.isInRange(i, binaryRanges) == false) {
|
if (isSeparator(char, dataString, separator, i, binaryRanges)) {
|
||||||
if (dataString[i - 1] != Separators.MaskingCharacterChar) {
|
parts.add(part.toString())
|
||||||
parts.add(extractPart(dataString, lastEndIndex, i, separator, partContainsMaskedSeparators))
|
|
||||||
|
|
||||||
lastEndIndex = i + 1
|
part = StringBuilder()
|
||||||
partContainsMaskedSeparators = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (char == Separators.MaskingCharacterChar && messageUtils.isInRange(i, binaryRanges) == false && i + 1 < dataString.length && dataString[i + 1] == separator) {
|
else if (isNotMaskingCharacter(char, dataString, separator, i, binaryRanges)) {
|
||||||
partContainsMaskedSeparators = true
|
part.append(char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastEndIndex != dataString.length) {
|
if (part.isNotEmpty()) {
|
||||||
parts.add(extractPart(dataString, lastEndIndex, dataString.length, separator, partContainsMaskedSeparators))
|
parts.add(part.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
return parts
|
return parts
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun extractPart(dataString: String, startIndex: Int, lastEndIndex: Int, separator: Char,
|
protected open fun isSeparator(char: Char, dataString: String, separator: Char, index: Int, binaryRanges: List<IntRange>): Boolean {
|
||||||
partContainsMaskedSeparators: Boolean): String {
|
return char == separator
|
||||||
|
&& (index == 0 || dataString[index - 1] != Separators.MaskingCharacterChar)
|
||||||
|
&& messageUtils.isInRange(index, binaryRanges) == false
|
||||||
|
}
|
||||||
|
|
||||||
val part = dataString.substring(startIndex, lastEndIndex)
|
protected open fun isNotMaskingCharacter(char: Char, dataString: String, separator: Char, index: Int, binaryRanges: List<IntRange>): Boolean {
|
||||||
|
return char != Separators.MaskingCharacterChar
|
||||||
if (partContainsMaskedSeparators) {
|
|| index + 1 >= dataString.length
|
||||||
return part.replace(Separators.MaskingCharacter + separator, separator.toString())
|
|| dataString[index + 1] != separator
|
||||||
}
|
|| messageUtils.isInRange(index, binaryRanges)
|
||||||
else {
|
|
||||||
return part
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue