func parseDependencyFileList()

in Sources/XCRemoteCache/Dependencies/DependenciesReader.swift [117:170]


    func parseDependencyFileList(_ string: String) -> [String] {
        var result: [String] = []
        var prevChar: UTF8.CodeUnit?

        // These index are used to move over the UTF8View of the string
        // The goal is to optimize the memory used, since UTF8View uses
        // the same memory as the original String without copying it
        var startIndex = string.utf8.startIndex
        var endIndex = startIndex

        // This buffer is only used to save the part of the path that has been already parsed when finding a backslash
        var buffer: String = ""

        for c in string.utf8 {
          switch c {
          case UTF8.CodeUnit(ascii: "\n") where prevChar == UTF8.CodeUnit(ascii: "\\"):
              startIndex = string.utf8.index(after: startIndex)
              endIndex = startIndex
          case UTF8.CodeUnit(ascii: " ") where startIndex == endIndex && buffer.isEmpty:
              startIndex = string.utf8.index(after: startIndex)
              endIndex = startIndex
          case UTF8.CodeUnit(ascii: " ") where prevChar != UTF8.CodeUnit(ascii: "\\"):
              // If a space is found and it is not escaped, then that's the end of the file path
              buffer += String(Substring(string.utf8[startIndex ..< endIndex]))
              result.append(buffer)
              buffer = ""
              prevChar = nil
              startIndex = string.utf8.index(after: endIndex)
              endIndex = startIndex
          case UTF8.CodeUnit(ascii: "\\"):
              // If a backslash is found it is not included in the file path
              // The current parsed range of the UTF8View is saved in the buffer as a String
              buffer += String(Substring(string.utf8[startIndex ..< endIndex]))
              // The backslash is assigned as the previous char
              prevChar = c
              // The indexes are moved to the next char so we continue parsing the String
              startIndex = string.utf8.index(after: endIndex)
              endIndex = startIndex
          default:
              // As long as it is possible the indexes are used to track the range of the string that
              // will be included in the file path (until it ends or until a backslash is found)
              endIndex = string.utf8.index(after: endIndex)
              // The char is assigned as the previous char
              prevChar = c
          }
        }

        if startIndex != endIndex {
            buffer += String(Substring(string.utf8[startIndex ..< endIndex]))
            result.append(buffer)
        }

        return result
    }