1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: add strings.Builder.write_string and use write_string instead of write (#8892)

This commit is contained in:
zakuro
2021-02-22 20:18:11 +09:00
committed by GitHub
parent 36a6bc270c
commit f54c1a5cc2
34 changed files with 402 additions and 397 deletions

View File

@@ -4,12 +4,12 @@ import strings
fn generate_temp_html() string {
mut temp_html := strings.new_builder(200)
temp_html.write('<!doctype html><html><head><title>Giant String</title></head><body>')
temp_html.write_string('<!doctype html><html><head><title>Giant String</title></head><body>')
for counter := 0; counter < 4; counter++ {
temp_html.write("<div id='name_$counter' ")
temp_html.write("class='several-$counter'>Look at $counter</div>")
temp_html.write_string("<div id='name_$counter' ")
temp_html.write_string("class='several-$counter'>Look at $counter</div>")
}
temp_html.write('</body></html>')
temp_html.write_string('</body></html>')
return temp_html.str()
}

View File

@@ -23,11 +23,11 @@ fn test_split_parse() {
fn test_giant_string() {
mut temp_html := strings.new_builder(200)
mut parser := Parser{}
temp_html.write('<!doctype html><html><head><title>Giant String</title></head><body>')
temp_html.write_string('<!doctype html><html><head><title>Giant String</title></head><body>')
for counter := 0; counter < 2000; counter++ {
temp_html.write("<div id='name_$counter' class='several-$counter'>Look at $counter</div>")
temp_html.write_string("<div id='name_$counter' class='several-$counter'>Look at $counter</div>")
}
temp_html.write('</body></html>')
temp_html.write_string('</body></html>')
parser.parse_html(temp_html.str())
assert parser.tags.len == 4009
}

View File

@@ -38,35 +38,35 @@ pub fn (tag Tag) text() string {
return '\n'
}
mut text_str := strings.new_builder(200)
text_str.write(tag.content.replace('\n', ''))
text_str.write_string(tag.content.replace('\n', ''))
for child in tag.children {
text_str.write(child.text())
text_str.write_string(child.text())
}
return text_str.str()
}
pub fn (tag &Tag) str() string {
mut html_str := strings.new_builder(200)
html_str.write('<$tag.name')
html_str.write_string('<$tag.name')
for key, value in tag.attributes {
html_str.write(' $key')
html_str.write_string(' $key')
if value.len > 0 {
html_str.write('="$value"')
html_str.write_string('="$value"')
}
}
html_str.write(if tag.closed && tag.close_type == .in_name {
html_str.write_string(if tag.closed && tag.close_type == .in_name {
'/>'
} else {
'>'
})
html_str.write(tag.content)
html_str.write_string(tag.content)
if tag.children.len > 0 {
for child in tag.children {
html_str.write(child.str())
html_str.write_string(child.str())
}
}
if !tag.closed || tag.close_type == .new_tag {
html_str.write('</$tag.name>')
html_str.write_string('</$tag.name>')
}
return html_str.str()
}

View File

@@ -65,7 +65,7 @@ pub fn decode(text string) string {
break
}
cscanner.skip_crlf()
sb.write(cscanner.read_chunk(csize))
sb.write_string(cscanner.read_chunk(csize))
cscanner.skip_crlf()
}
cscanner.skip_crlf()

View File

@@ -204,12 +204,12 @@ pub fn (c &Cookie) str() string {
// see RFC 6265 Sec 4.1.
extra_cookie_length := 110
mut b := strings.new_builder(c.name.len + c.value.len + c.domain.len + c.path.len + extra_cookie_length)
b.write(c.name)
b.write('=')
b.write(sanitize_cookie_value(c.value))
b.write_string(c.name)
b.write_string('=')
b.write_string(sanitize_cookie_value(c.value))
if c.path.len > 0 {
b.write('; path=')
b.write(sanitize_cookie_path(c.path))
b.write_string('; path=')
b.write_string(sanitize_cookie_path(c.path))
}
if c.domain.len > 0 {
if valid_cookie_domain(c.domain) {
@@ -221,8 +221,8 @@ pub fn (c &Cookie) str() string {
if d[0] == `.` {
d = d.substr(1, d.len)
}
b.write('; domain=')
b.write(d)
b.write_string('; domain=')
b.write_string(d)
} else {
// TODO: Log invalid cookie domain warning
}
@@ -230,35 +230,35 @@ pub fn (c &Cookie) str() string {
if c.expires.year > 1600 {
e := c.expires
time_str := '${e.weekday_str()}, ${e.day.str()} ${e.smonth()} ${e.year} ${e.hhmmss()} GMT'
b.write('; expires=')
b.write(time_str)
b.write_string('; expires=')
b.write_string(time_str)
}
// TODO: Fix this. Techically a max age of 0 or less should be 0
// We need a way to not have a max age.
if c.max_age > 0 {
b.write('; Max-Age=')
b.write(c.max_age.str())
b.write_string('; Max-Age=')
b.write_string(c.max_age.str())
} else if c.max_age < 0 {
b.write('; Max-Age=0')
b.write_string('; Max-Age=0')
}
if c.http_only {
b.write('; HttpOnly')
b.write_string('; HttpOnly')
}
if c.secure {
b.write('; Secure')
b.write_string('; Secure')
}
match c.same_site {
.same_site_default_mode {
b.write('; SameSite')
b.write_string('; SameSite')
}
.same_site_none_mode {
b.write('; SameSite=None')
b.write_string('; SameSite=None')
}
.same_site_lax_mode {
b.write('; SameSite=Lax')
b.write_string('; SameSite=Lax')
}
.same_site_strict_mode {
b.write('; SameSite=Strict')
b.write_string('; SameSite=Strict')
}
}
return b.str()

View File

@@ -145,9 +145,9 @@ fn (mut c Client) send_auth() ? {
}
mut sb := strings.new_builder(100)
sb.write_b(0)
sb.write(c.username)
sb.write_string(c.username)
sb.write_b(0)
sb.write(c.password)
sb.write_string(c.password)
a := sb.str()
auth := 'AUTH PLAIN ${base64.encode(a)}\r\n'
c.send_str(auth) ?
@@ -173,18 +173,18 @@ fn (mut c Client) send_body(cfg Mail) ? {
is_html := cfg.body_type == .html
date := cfg.date.utc_string().trim_right(' UTC') // TODO
mut sb := strings.new_builder(200)
sb.write('From: $cfg.from\r\n')
sb.write('To: <$cfg.to>\r\n')
sb.write('Cc: <$cfg.cc>\r\n')
sb.write('Bcc: <$cfg.bcc>\r\n')
sb.write('Date: $date\r\n')
sb.write('Subject: $cfg.subject\r\n')
sb.write_string('From: $cfg.from\r\n')
sb.write_string('To: <$cfg.to>\r\n')
sb.write_string('Cc: <$cfg.cc>\r\n')
sb.write_string('Bcc: <$cfg.bcc>\r\n')
sb.write_string('Date: $date\r\n')
sb.write_string('Subject: $cfg.subject\r\n')
if is_html {
sb.write('Content-Type: text/html; charset=ISO-8859-1')
sb.write_string('Content-Type: text/html; charset=ISO-8859-1')
}
sb.write('\r\n\r\n')
sb.write(cfg.body)
sb.write('\r\n.\r\n')
sb.write_string('\r\n\r\n')
sb.write_string(cfg.body)
sb.write_string('\r\n.\r\n')
c.send_str(sb.str()) ?
c.expect_reply(.action_ok) ?
}

View File

@@ -209,18 +209,18 @@ fn unescape(s_ string, mode EncodingMode) ?string {
x := s[i]
match x {
`%` {
t.write(((unhex(s[i + 1]) << byte(4)) | unhex(s[i + 2])).ascii_str())
t.write_string(((unhex(s[i + 1]) << byte(4)) | unhex(s[i + 2])).ascii_str())
i += 2
}
`+` {
if mode == .encode_query_component {
t.write(' ')
t.write_string(' ')
} else {
t.write('+')
t.write_string('+')
}
}
else {
t.write(s[i].ascii_str())
t.write_string(s[i].ascii_str())
}
}
}
@@ -715,27 +715,27 @@ fn valid_optional_port(port string) bool {
pub fn (u URL) str() string {
mut buf := strings.new_builder(200)
if u.scheme != '' {
buf.write(u.scheme)
buf.write(':')
buf.write_string(u.scheme)
buf.write_string(':')
}
if u.opaque != '' {
buf.write(u.opaque)
buf.write_string(u.opaque)
} else {
if u.scheme != '' || u.host != '' || (u.user != 0 && !u.user.empty()) {
if u.host != '' || u.path != '' || !u.user.empty() {
buf.write('//')
buf.write_string('//')
}
if !u.user.empty() {
buf.write(u.user.str())
buf.write('@')
buf.write_string(u.user.str())
buf.write_string('@')
}
if u.host != '' {
buf.write(escape(u.host, .encode_host))
buf.write_string(escape(u.host, .encode_host))
}
}
path := u.escaped_path()
if path != '' && path[0] != `/` && u.host != '' {
buf.write('/')
buf.write_string('/')
}
if buf.len == 0 {
// RFC 3986 §4.2
@@ -746,18 +746,18 @@ pub fn (u URL) str() string {
// path reference.
i := path.index_byte(`:`)
if i > -1 && path[..i].index_byte(`/`) == -1 {
buf.write('./')
buf.write_string('./')
}
}
buf.write(path)
buf.write_string(path)
}
if u.force_query || u.raw_query != '' {
buf.write('?')
buf.write(u.raw_query)
buf.write_string('?')
buf.write_string(u.raw_query)
}
if u.fragment != '' {
buf.write('#')
buf.write(escape(u.fragment, .encode_fragment))
buf.write_string('#')
buf.write_string(escape(u.fragment, .encode_fragment))
}
return buf.str()
}
@@ -845,11 +845,11 @@ pub fn (v Values) encode() string {
key_kscaped := query_escape(k)
for _, val in vs.data {
if buf.len > 0 {
buf.write('&')
buf.write_string('&')
}
buf.write(key_kscaped)
buf.write('=')
buf.write(query_escape(val))
buf.write_string(key_kscaped)
buf.write_string('=')
buf.write_string(query_escape(val))
}
}
return buf.str()