Call for Users (and Authors): Offset indexing pitch


(Michael Ilseman) #61

This is a huge part of the motivation behind Contiguous Strings, which is now in review.


(Tellow Krinkle) #62

Here's what I would do if I were to use an ASCII String version of my string scanner to parse:

func parse(pktLine: ASCIIString) throws {
	var scanner = Scanner(pktLine)
	guard scanner.read(count: 4).flatMap({ Int($0, radix: 16) }) == pktLine.count else {
		throw ParseException("Bad length")
	}
	guard let requestCommand = scanner.read(toNext: " "),
	      ["git-upload-pack", "git-receive-pack", "git-upload-archive"].contains(requestCommand)
	else {
		throw ParseException("Bad request-command")
	}
	print("request-command: \(requestCommand)")
	guard let pathname = scanner.read(toNext: "\0") else {
		throw ParseException("Missing pathname")
	}
	print("pathname: \(pathname)")
	if scanner.isEmpty { return }

	guard scanner.remove(prefix: "host=") else {
		throw ParseException("Bad host-parameter")
	}
	guard var hostnameAndPort = scanner.read(toNext: "\0").map(Scanner.init), !hostnameAndPort.isEmpty else {
		throw ParseException("Missing hostname")
	}
	let hostname = hostnameAndPort.read(toNext: ":")!
	let port = hostnameAndPort.rest
	print("hostname: \(hostname)")
	port.map { print("port: \($0)") }

	guard let extraPrefix = scanner.read(toNext: "\0") else { return }
	guard extraPrefix.isEmpty else {
		throw ParseException("bad extra-parameters")
	}
	while let extraParameter = scanner.read(toNext: "\0") {
		print("extra-parameter: \(extraParameter)")
	}
}

(Chris Lattner) #63

Have you considered the potential confusion where array slices will sometimes have completely different behavior for:

A[4] // may actually be the first element of the slice if the startIndex is 4.
A[offset: 4] // Always startIndex+4

? I haven't read all the comments in detail, so I'm sorry if someone already asked this.

-Chris


(Michael Ilseman) #64

Yes, that's been discussed in this thread and I briefly alluded to to in the pitch and examples:

This is worth elaborating on with code examples (especially generic functions over RAC) in a new iteration of the pitch.