Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created November 3, 2017 01:20
Show Gist options
  • Save jemygraw/abeda8854406069409085a645e39e8c5 to your computer and use it in GitHub Desktop.
Save jemygraw/abeda8854406069409085a645e39e8c5 to your computer and use it in GitHub Desktop.
import scala.collection.mutable.ListBuffer
import scala.util.control.Breaks
def parseCdnLogLineV2(line: String): Array[String] = {
val itemBuffer = new ListBuffer[String]()
val length = line.length
var startIndex = 0
var delimiter = " "
var elemStart = ""
val loop = new Breaks
var elemIndex = 0
loop.breakable {
while (true) {
var endIndex = line.indexOf(delimiter, startIndex)
if (endIndex == -1) {
loop.break()
}
if (elemIndex == 4) {
itemBuffer ++= line.substring(startIndex, endIndex).split(" ")
} else {
itemBuffer += line.substring(startIndex, endIndex)
}
if (delimiter == "]" || delimiter == "\"") {
endIndex = endIndex + 1
}
if (endIndex + 1 >= length) {
loop.break()
}
var token = line.charAt(endIndex + 1)
if (token == '[') {
delimiter = "]"
startIndex = endIndex + 2
} else if (token == '"') {
delimiter = "\""
startIndex = endIndex + 2
} else {
delimiter = " "
startIndex = endIndex + 1
}
elemIndex = elemIndex + 1
}
}
itemBuffer.toArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment