- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试实现我自己的 beanstalkd 客户端作为学习围棋的一种方式。 https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
目前,我正在使用 bufio
读取由 \n
分隔的一行数据.
res, err := this.reader.ReadLine('\n')
当我发送单个命令并读取单行响应时,这很好:INSERTED %d\r\n
但是当我尝试保留工作时我发现困难,因为工作主体可能是多行,因此我不能使用 \n
分隔符。
有没有办法读入缓冲区直到CRLF
?
例如当我发送 reserve
命令。我的预期响应如下:
RESERVED <id> <bytes>\r\n
<data>\r\n
但数据可能包含 \n
, 所以我需要读到 \r\n
.
或者 - 有没有一种方法可以读取 <bytes>
中指定的特定字节数?在上面的示例响应中?
目前,我有(删除错误处理):
func (this *Bean) receiveLine() (string, error) {
res, err := this.reader.ReadString('\n')
return res, err
}
func (this *Bean) receiveBody(numBytesToRead int) ([]byte, error) {
res, err := this.reader.ReadString('\r\n') // What to do here to read to CRLF / up to number of expected bytes?
return res, err
}
func (this *Bean) Reserve() (*Job, error) {
this.send("reserve\r\n")
res, err := this.receiveLine()
var jobId uint64
var bodylen int
_, err = fmt.Sscanf(res, "RESERVED %d %d\r\n", &jobId, &bodylen)
body, err := this.receiveBody(bodylen)
job := new(Job)
job.Id = jobId
job.Body = body
return job, nil
}
最佳答案
res, err := this.reader.Read('\n')
对我来说没有任何意义。您是指 ReadBytes/ReadSlice/ReadString 吗?
你需要 bufio.Scanner。
定义您的 bufio.SplitFunc(示例是 bufio.ScanLines 的副本,并进行了修改以查找“\r\n”)。修改它以符合您的情况。
// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}
func ScanCRLF(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte{'\r','\n'}); i >= 0 {
// We have a full newline-terminated line.
return i + 2, dropCR(data[0:i]), nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), dropCR(data), nil
}
// Request more data.
return 0, nil, nil
}
现在,用您的自定义扫描仪包装您的 io.Reader。
scanner := bufio.NewScanner(this.reader)
scanner.Split(ScanCRLF)
// Set the split function for the scanning operation.
scanner.Split(split)
// Validate the input
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf("Invalid input: %s", err)
}
阅读bufio package's Scanner源码。
Alternatively - is there a way of reading a specific number of bytes as specified in in example response above?
首先你需要阅读“RESERVED\r\n”行。
然后你就可以使用
nr_of_bytes : = read_number_of_butes_somehow(this.reader)
buf : = make([]byte, nr_of_bytes)
this.reader.Read(buf)
但我不喜欢这种方法。
Thanks for this - reader.Read('\n') was a typo - I corrected question. I have also attached example code of where I have got so far. As you can see, I can get the number of expected bytes of the body. Could you elaborate on why you don't like the idea of reading a specific number of bytes? This seems most logical?
我想看看 Bean 的定义,尤其是读者的部分。想象一下,这个计数器不知何故是错误的。
它很简短:您需要找到后面的“\r\n”并丢弃到那一点为止的所有内容?或不?那你为什么首先需要柜台?
它比它应该的更大(或者更糟的是它巨大!)。
2.1 阅读器中没有下一条消息:很好,阅读比预期的要短,但没问题。
2.2 有下一条消息等待:呸,你读了一部分,没有简单的方法可以恢复。
2.3 巨大:即使消息只有 1 个字节,也无法分配内存。
这个字节计数器一般是用来验证消息的。看起来 beanstalkd 协议(protocol)就是这种情况。
使用 Scanner,解析消息,检查长度与预期数字......利润
UPD
请注意,默认的 bufio.Scanner 无法读取超过 64k 的数据,请先使用 scanner.Buffer 设置最大长度。这很糟糕,因为您不能即时更改此选项,并且某些数据可能已被扫描仪“预”读取。
UPD2
思考我的最后更新。看看net.textproto它如何像简单状态机一样实现 dotReader。您可以先读取命令并检查有效负载的“预期字节数”来做类似的事情。
关于golang - bufio 读取多行直到 (CRLF)\r\n 定界符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37530451/
我有一列data.frame,其中一些空格应该作为分隔符,而某些只是一个空格。 #input data dat <- data.frame(x=c("A 2 2 textA1 textA2 Z1",
所以我试图分解一个包含答案列表的字符串。 例如:答案:1. 梳理。 2.拇指。 3.坟墓(地下墓穴)。 4. 子宫。 5.面包屑。 6. 炸弹。 7. NumPy 。 8.沉着冷静。 9. 屈服。 有
我有包含多个查询的文件。如何使用 DELIMITER 将它们分开?我像在 mysql 中一样尝试过,但它没有编译: DELIMITER % CREATE SEQUENCE logins_seq I
这个问题在这里已经有了答案: Does -* have any special meaning in regular expression? (1 个回答) 关闭 8 年前。 我有一个像 22 +
我正在尝试使用 sscanf 解析以下字符串: query=testword&diskimg=simple.img 如何使用 sscanf 解析出“testword”和“simple.img”? ss
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示: foo 4.500 bar 3.00 1.3 3 foo bar 如何读取由空格而不是换行符分隔的文件?有什么方法可以设置
这是我的代码: With ad.Tables(2) For i As Integer = 0 To .Rows.Count - 1 If .Rows(i)("name") &
我正在排队,例如 数组大小:4 来自 cin,我想首先检查字符串是否正是这个,然后提取整数。 我找到了读取字符串并提取整数的方法: string start; getline (cin
我正在尝试读取以这种形式编写的文本文件: AB523:[joe, pierre][charlie][dogs,cat] ZZ883:[ronald, zigomarre][pele]
我是 PIG 的新手,有人可以帮助我如何加载一个包含多个字符(在我的例子中为“^^”)作为列分隔符的文件。 例如我有以下列的文件aisforapple^^bisforball^^cisforcat^^
var wrapped_string = "shouldn't this\ work?" JSLint给出了严重的擒纵机构错误。 那么我们如何在 JavaScript 中执行heredoc呢?我有一个
问题:我需要读取 CSV 文件。我使用 FileHelpers 库来实现这一点。 问题是我需要一个动态定界符(用户定义),这意味着任何东西都可以是定界符(逗号、分号、制表符、换行符,以及其他任何东西)
我试图读入一个由 powershell 执行的 sql 文件。一切正常,除了 powershell 因单词定界符而抛出错误。所以我的问题是如何在我的 sql 文件中使用定界符(我只能对 sql 文件进
我需要能够通过 DBI 发出“创建触发器”。我似乎无法使分隔符命令正常工作。任何人都可以找到一种方法来完成这项工作吗? 代码: use strict; use DBI; my $dbargs = {m
我知道我可以使用 Mustache.tags('[[', ']]'); 更改默认分隔符 我深入研究了源代码,但找不到也不知道如何更改不转义 HTML 定界符,默认情况下为 {{{ }}}。感谢您的帮助
我正在尝试获取 std::string 值并将其用作 std::getline() 函数中的分隔符但无论我做什么,编译器都会给我错误。这就是我想要做的: std::stringstream ss(s)
我正在尝试实现我自己的 beanstalkd 客户端作为学习围棋的一种方式。 https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
我正在编写一个显示代码和输出的样式指南。它目前的结构使得代码只需要描述一次,并以其原始版本和解释版本显示,如下所示: #{ image_tag 'image.png' } PLACE_THE
是否可以将默认字段分隔符从逗号更改为其他字符,例如用于导出的 '|'? 最佳答案 下面是一个使用选项卡的示例。 到一个文件: CSV.open("myfile.csv", "w", {:col_sep
我是一名优秀的程序员,十分优秀!