Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3bae1c

Browse files
committed
simpler truncating
1 parentc3bf516 commitc3bae1c

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

‎coderd/coderd.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,9 @@ func FmtWebsocketCloseMsg(format string, vars ...any) string {
212212
// Cap msg length at 123 bytes. nhooyr/websocket only allows close messages
213213
// of this length.
214214
iflen(msg)>websocketCloseMaxLen {
215-
// truncateString safely truncates a string to a maximum size of byteLen. It
216-
// writes whole runes until a single rune would increase the string size above
217-
// byteLen.
218-
truncateString:=func(strstring,byteLenint)string {
219-
builder:= strings.Builder{}
220-
builder.Grow(byteLen)
221-
222-
for_,char:=rangestr {
223-
ifbuilder.Len()+len(string(char))>byteLen {
224-
break
225-
}
226-
227-
_,_=builder.WriteRune(char)
228-
}
229-
230-
returnbuilder.String()
231-
}
232-
233-
returntruncateString(msg,websocketCloseMaxLen)
215+
// Trim the string to 123 bytes. If we accidentally cut in the middle of
216+
// a UTF-8 character, remove it from the string.
217+
returnstrings.ToValidUTF8(string(msg[123]),"")
234218
}
235219

236220
returnmsg

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp