scala BufferedSource 位置
scala BufferedSource Position
我有 2 个关于 Scala 的问题classBufferedSource
。
如何查询当前阅读位置?
我用 pos
试过了:
object testapp extends App {
val doc = scala.io.Source.fromFile("text.txt")
println(doc.pos)
doc.next()
println(doc.pos)
doc.next()
println(doc.pos)
}
输出:
0
2049
2050
为什么它从 0 跳到 2049?!
是否有另一种查询位置的方法and/or将其设置在其他地方?
感谢您的帮助:-)
pos
returns next()
返回的最后一个字符的位置,但诀窍是位置是由位置编码器编码的行和列的组合(scala.io.Position
) 作为单个 Integer
:
The object Position provides convenience methods to encode * line
and column number in one single integer. The encoded line *
(column) numbers range from 0 to LINE_MASK
(COLUMN_MASK
), *
where 0
indicates that the line (column) is undefined and * 1
represents the first line (column)...
https://github.com/scala/scala/blob/v2.11.8/src/library/scala/io/Position.scala
http://www.scala-lang.org/api/2.11.8/#scala.io.Source$RelaxedPosition$
使用Postioner
以获得更易读的信息:
The current input and position, as well as the next character methods
delegate to the positioner.
示例:
val doc = scala.io.Source.fromFile("aaa.txt")
val positioner = new doc.Positioner()
val positioned = doc.withPositioning(positioner)
positioned.next()
scala> positioner.cline -> positioner.ccol
res15: (Int, Int) = (1,2)
positioned.next()
scala> positioner.cline -> positioner.ccol
res17: (Int, Int) = (1,3)
P.S。 Source
旨在以字符流的形式读取数据,因此它为您提供了 getLines()
等便利,所以基本上这就是 Positioner
使用行和列而不是绝对位置的原因。
如果你需要一个Iterator
,returns你每个字符的绝对位置,使用zipWithIndex
:
scala> val doc = scala.io.Source.fromFile("aaa.txt").zipWithIndex
doc: Iterator[(Char, Int)] = non-empty iterator
scala> doc.next()
res38: (Char, Int) = (a,0)
scala> doc.next()
res39: (Char, Int) = (a,1)
scala> doc.next()
res40: (Char, Int) = (a,2)
scala> doc.next()
res41: (Char, Int) = (a,3)
scala> doc.next()
res42: (Char, Int) =
(
,4)
我有 2 个关于 Scala 的问题classBufferedSource
。
如何查询当前阅读位置?
我用 pos
试过了:
object testapp extends App {
val doc = scala.io.Source.fromFile("text.txt")
println(doc.pos)
doc.next()
println(doc.pos)
doc.next()
println(doc.pos)
}
输出:
0
2049
2050
为什么它从 0 跳到 2049?!
是否有另一种查询位置的方法and/or将其设置在其他地方?
感谢您的帮助:-)
pos
returns next()
返回的最后一个字符的位置,但诀窍是位置是由位置编码器编码的行和列的组合(scala.io.Position
) 作为单个 Integer
:
The object Position provides convenience methods to encode * line and column number in one single integer. The encoded line * (column) numbers range from 0 to
LINE_MASK
(COLUMN_MASK
), * where0
indicates that the line (column) is undefined and *1
represents the first line (column)...https://github.com/scala/scala/blob/v2.11.8/src/library/scala/io/Position.scala
http://www.scala-lang.org/api/2.11.8/#scala.io.Source$RelaxedPosition$
使用Postioner
以获得更易读的信息:
The current input and position, as well as the next character methods delegate to the positioner.
示例:
val doc = scala.io.Source.fromFile("aaa.txt")
val positioner = new doc.Positioner()
val positioned = doc.withPositioning(positioner)
positioned.next()
scala> positioner.cline -> positioner.ccol
res15: (Int, Int) = (1,2)
positioned.next()
scala> positioner.cline -> positioner.ccol
res17: (Int, Int) = (1,3)
P.S。 Source
旨在以字符流的形式读取数据,因此它为您提供了 getLines()
等便利,所以基本上这就是 Positioner
使用行和列而不是绝对位置的原因。
如果你需要一个Iterator
,returns你每个字符的绝对位置,使用zipWithIndex
:
scala> val doc = scala.io.Source.fromFile("aaa.txt").zipWithIndex
doc: Iterator[(Char, Int)] = non-empty iterator
scala> doc.next()
res38: (Char, Int) = (a,0)
scala> doc.next()
res39: (Char, Int) = (a,1)
scala> doc.next()
res40: (Char, Int) = (a,2)
scala> doc.next()
res41: (Char, Int) = (a,3)
scala> doc.next()
res42: (Char, Int) =
(
,4)