mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
run vfmt on http, net, sync, strconv
This commit is contained in:
@@ -1,27 +1,29 @@
|
||||
module chunked
|
||||
|
||||
import strings
|
||||
|
||||
// See: https://en.wikipedia.org/wiki/Chunked_transfer_encoding
|
||||
///////////////////////////////////////////////////////////////
|
||||
// The chunk size is transferred as a hexadecimal number
|
||||
// followed by \r\n as a line separator,
|
||||
// /////////////////////////////////////////////////////////////
|
||||
// The chunk size is transferred as a hexadecimal number
|
||||
// followed by \r\n as a line separator,
|
||||
// followed by a chunk of data of the given size.
|
||||
// The end is marked with a chunk with size 0.
|
||||
|
||||
struct ChunkScanner {
|
||||
mut:
|
||||
pos int
|
||||
pos int
|
||||
text string
|
||||
}
|
||||
|
||||
fn (s mut ChunkScanner) read_chunk_size() int {
|
||||
mut n := 0
|
||||
for {
|
||||
if s.pos >= s.text.len { break }
|
||||
if s.pos >= s.text.len {
|
||||
break
|
||||
}
|
||||
c := s.text[s.pos]
|
||||
if !c.is_hex_digit() { break }
|
||||
n = n << 4
|
||||
if !c.is_hex_digit() {
|
||||
break
|
||||
}
|
||||
n = n<<4
|
||||
n += int(unhex(c))
|
||||
s.pos++
|
||||
}
|
||||
@@ -29,9 +31,15 @@ fn (s mut ChunkScanner) read_chunk_size() int {
|
||||
}
|
||||
|
||||
fn unhex(c byte) byte {
|
||||
if `0` <= c && c <= `9` { return c - `0` }
|
||||
else if `a` <= c && c <= `f` { return c - `a` + 10 }
|
||||
else if `A` <= c && c <= `F` { return c - `A` + 10 }
|
||||
if `0` <= c && c <= `9` {
|
||||
return c - `0`
|
||||
}
|
||||
else if `a` <= c && c <= `f` {
|
||||
return c - `a` + 10
|
||||
}
|
||||
else if `A` <= c && c <= `F` {
|
||||
return c - `A` + 10
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -47,17 +55,20 @@ fn (s mut ChunkScanner) read_chunk(chunksize int) string {
|
||||
|
||||
pub fn decode(text string) string {
|
||||
mut sb := strings.new_builder(100)
|
||||
mut cscanner := ChunkScanner {
|
||||
mut cscanner := ChunkScanner{
|
||||
pos: 0
|
||||
text: text
|
||||
}
|
||||
for {
|
||||
csize := cscanner.read_chunk_size()
|
||||
if 0 == csize { break }
|
||||
if 0 == csize {
|
||||
break
|
||||
}
|
||||
cscanner.skip_crlf()
|
||||
sb.write( cscanner.read_chunk(csize) )
|
||||
sb.write(cscanner.read_chunk(csize))
|
||||
cscanner.skip_crlf()
|
||||
}
|
||||
cscanner.skip_crlf()
|
||||
return sb.str()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user