Last active
August 12, 2020 17:47
-
-
Save quetool/d64119777badf1eda399caa0af10da8a to your computer and use it in GitHub Desktop.
Dart translation from https://github.com/wardaLyns/EMVCoParser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EMVCoParser Dart translation from https://github.com/wardaLyns/EMVCoParser | |
class EMVCoParser { | |
static final int length = 2; | |
String emvcoQrValue; | |
int tag, subTag; | |
bool hasSubTag; | |
EMVCoParser(String emvcoQrValue, int tag) { | |
this.emvcoQrValue = emvcoQrValue; | |
this.tag = tag; | |
} | |
EMVCoParser.withSubtag({String emvcoQrValue, int tag, int subTag}) { | |
this.emvcoQrValue = emvcoQrValue; | |
this.tag = tag; | |
this.subTag = subTag; | |
hasSubTag = true; | |
} | |
String getParsingValue() { | |
Map<int, EmvObject> parsingValues = parseEmvcoQR(emvcoQrValue); | |
EmvObject obj = parsingValues[this.tag]; | |
if (hasSubTag) { | |
return parseEmvcoQR(obj.getValues())[this.subTag].getValues(); | |
} | |
return obj.getValues(); | |
} | |
Map<int, EmvObject> parseEmvcoQR(String str) { | |
Map<int, EmvObject> result = new Map<int, EmvObject>(); | |
int i = 0; | |
while (i < str.length) { | |
int tagInd = i + length; | |
int tag = int.parse(str.substring(i, tagInd)); | |
int lengthValue = int.parse(str.substring(tagInd, tagInd + length)); | |
EmvObject emvObject = new EmvObject(); | |
emvObject.setLength(lengthValue); | |
emvObject.setValues(str.substring(tagInd + length, tagInd + length + lengthValue)); | |
print("Index Tag/SubTag : " + | |
tagInd.toString() + | |
" Length : " + | |
lengthValue.toString() + | |
" Values " + | |
str.substring(tagInd + length, tagInd + length + lengthValue)); | |
// result.put(tag, emvObject); | |
result.putIfAbsent(tag, () => emvObject); | |
i = tagInd + length + lengthValue; | |
} | |
return result; | |
} | |
} | |
class EmvObject { | |
int length; | |
String values; | |
int getLength() { | |
return length; | |
} | |
void setLength(int length) { | |
this.length = length; | |
} | |
String getValues() { | |
return values; | |
} | |
void setValues(String values) { | |
this.values = values; | |
} | |
// bool equals(EmvObject o) { | |
// // if (this == o) return true; | |
// // if (o == null || getClass() != o.getClass()) return false; | |
// EmvObject emvObject = o; | |
// if (length != emvObject.length) return false; | |
// // return values != null ? values.equals(emvObject.values) : emvObject.values == null; | |
// return (values != null) ? values.compareTo(emvObject.values) : emvObject.values == null; | |
// } | |
// // @override | |
// int hashCode() { | |
// int result = length; | |
// result = 31 * result + (values != null ? values.hashCode : 0); | |
// return result; | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much sir!