本文整理汇总了Golang中bytes.SplitAfter函数的典型用法代码### 示例。如果您正苦于以下问题:Golang SplitAfter函数的具体用法?Golang SplitAfter怎么用?Golang SplitAfter使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。
在下文中一共展示了SplitAfter函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: splitByPreformatted
func splitByPreformatted(input []byte) fileBlocks {
f := fileBlocks{}
cur := []byte(nil)
preformatted := false
// SplitAfter keeps the newline, so you don't have to worry about
// omitting it on the last line or anything. Also, the documentation
// claims it's unicode safe.
for _, line := range bytes.SplitAfter(input, []byte("\n")) {
if !preformatted {
if preformatRE.Match(line) && !notPreformatRE.Match(line) {
if len(cur) > 0 {
f = append(f, fileBlock{false, cur})
}
cur = []byte{}
preformatted = true
}
cur = append(cur, line...)
} else {
cur = append(cur, line...)
if preformatEndRE.Match(line) {
if len(cur) > 0 {
f = append(f, fileBlock{true, cur})
}
cur = []byte{}
preformatted = false
}
}
}
if len(cur) > 0 {
f = append(f, fileBlock{preformatted, cur})
}
return f
}
开发者ID:jdoliner,项目名称:kubernetes,代码行数:34,代码来源:util.go
示例2: replaceNonPreformatted
// Calls 'replace' for all sections of the document not in ``` / ``` blocks. So
// that you don't have false positives inside those blocks.
func replaceNonPreformatted(input []byte, replace func([]byte) []byte) []byte {
output := []byte(nil)
cur := []byte(nil)
keepBlock := true
// SplitAfter keeps the newline, so you don't have to worry about
// omitting it on the last line or anything. Also, the documentation
// claims it's unicode safe.
for _, line := range bytes.SplitAfter(input, []byte("\n")) {
if keepBlock {
if preformatRE.Match(line) {
cur = replace(cur)
output = append(output, cur...)
cur = []byte{}
keepBlock = false
}
cur = append(cur, line...)
} else {
cur = append(cur, line...)
if preformatRE.Match(line) {
output = append(output, cur...)
cur = []byte{}
keepBlock = true
}
}
}
if keepBlock {
cur = replace(cur)
}
output = append(output, cur...)
return output
}
开发者ID:kcyeu,项目名称:kubernetes,代码行数:33,代码来源:util.go
示例3: Flush
//Flushes any fully formed messages out to the channel
func (conn *Connection) Flush() {
//split the messages in the buffer into individual messages
messages := bytes.SplitAfter(conn.buf, []byte{'\n'})
//if theres only one message, then theres no newline yet
//so continue to buffer
if len(messages) == 1 {
return
}
//chop off the last message because it's just a blank string
for _, message := range messages[:len(messages)-1] {
//attempt to send the message
if n, err := conn.SendMessage(string(message)); err != nil {
//if an error occurs, chop off the bit that was sent
conn.buf = conn.buf[n:]
return
}
//chop off the message from the buffer
conn.buf = conn.buf[len(message):]
}
return
}
开发者ID:zeebo,项目名称:irc,代码行数:26,代码来源:irc.go
示例4: main
func main() {
languages := []byte("golang haskell ruby python")
individualLanguages := bytes.Fields(languages)
log.Printf("Fields split %q on whitespace into %q", languages, individualLanguages)
vowelsAndSpace := "aeiouy "
split := bytes.FieldsFunc(languages, func(r rune) bool {
return strings.ContainsRune(vowelsAndSpace, r)
})
log.Printf("FieldsFunc split %q on vowels and space into %q", languages, split)
space := []byte{' '}
splitLanguages := bytes.Split(languages, space)
log.Printf("Split split %q on a single space into %q", languages, splitLanguages)
numberOfSubslices := 2 // Not number of splits
singleSplit := bytes.SplitN(languages, space, numberOfSubslices)
log.Printf("SplitN split %q on a single space into %d subslices: %q", languages, numberOfSubslices, singleSplit)
splitAfterLanguages := bytes.SplitAfter(languages, space)
log.Printf("SplitAfter split %q AFTER a single space (keeping the space) into %q", languages, splitAfterLanguages)
splitAfterNLanguages := bytes.SplitAfterN(languages, space, numberOfSubslices)
log.Printf("SplitAfterN split %q AFTER a single space (keeping the space) into %d subslices: %q", languages, numberOfSubslices, splitAfterNLanguages)
languagesBackTogether := bytes.Join(individualLanguages, space)
log.Printf("Languages are back togeher again! %q == %q? %v", languagesBackTogether, languages, bytes.Equal(languagesBackTogether, languages))
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:29,代码来源:splitjoin.go
示例5: ListenTo
func ListenTo(url string) (net.Conn, chan []byte) {
con, err := net.Dial("tcp", url)
deny(err)
ch := make(chan []byte)
go func() {
var replyBuffer bytes.Buffer
readBuffer := make([]byte, 1024)
for {
con.SetDeadline(time.Now().Add(time.Minute))
bytesRead, err := con.Read(readBuffer)
if err != nil {
close(ch)
log.Printf("ListenTo connection error: %s", err)
return
}
replyBuffer.Write(readBuffer[:bytesRead])
lines := bytes.SplitAfter(replyBuffer.Bytes(), []byte("\n"))
for _, line := range lines[:len(lines)-1] {
n := len(line)
if n > 1 {
lineCopy := make([]byte, n)
copy(lineCopy, line)
ch <- lineCopy
}
replyBuffer.Next(n)
}
}
}()
return con, ch
}
开发者ID:GoodGames,项目名称:ScrollsTradeBot,代码行数:33,代码来源:connection.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng