是否可以设置 Java CharBuffer 的位置?
对我正在尝试做的事情进行一些补充。
我想解析一个字符串,但能够进行比较,例如
- 位置 x 处的 1 个字符 位置
- x 处的 2 个字符
- ...
- 位置 x 处的 n 个字符
失败
- 位置 x+1 处的 1 个字符
- 位置 x+1 处的 2 个字符
- ...
- 位置 x+1 处的 n 个字符
等
继续,直到我得到匹配或 EOS。
使用数组很容易做到这一点,但我想在一个方法中执行此操作并返回一个缓冲区,该缓冲区的索引指向下一个开始处理的位置。我相信 CharBuffer 是一个很好的解决方案,但我不确定。
通过示例编辑 - 不完全是可编译的代码!
主
List template1 = new List();
List template2 = new List();
String exampleInput = "oneone two";
template1.add ( "one" );
template1.add ( "two" );
template1.add ( "three" );
template2.add ( "one" );
template2.add ( "one" );
template2.add ( "two" );
Set templates = new Set();
templates.add ( template1 );
templates.add ( template2 );
NoisyParser np = new NoisyParser();
np.parse( templates, exampleInput );
噪声解析器
void parse( Set templates, Sting inp ){
iterate over each template that matches inp{
find_match( template, inp );
}
}
boolean find_match( template, inp ) {
This is where I need the magic to work out if the current element
in template matches the current position of inp, or inp+1, give or take.
}
A bit of backfill on what I am trying to do.
I want to parse a String, but to be able to compare, for example
- 1 char at position x
- 2 chars at position x
- ...
- n chars at position x
failing that
- 1 char at position x+1
- 2 chars at position x+1
- ...
- n chars at position x+1
etc
continuing until I get a match, or EOS.
It's easy to do this with an array but I want to do this in a method and return a buffer with an index pointing to the next place to start processing. I was led to believe CharBuffer was a good solution for this, but I'm not sure.
EDIT by way of an example - not exactly compile-able code!
Main
List template1 = new List();
List template2 = new List();
String exampleInput = "oneone two";
template1.add ( "one" );
template1.add ( "two" );
template1.add ( "three" );
template2.add ( "one" );
template2.add ( "one" );
template2.add ( "two" );
Set templates = new Set();
templates.add ( template1 );
templates.add ( template2 );
NoisyParser np = new NoisyParser();
np.parse( templates, exampleInput );
NoisyParser
void parse( Set templates, Sting inp ){
iterate over each template that matches inp{
find_match( template, inp );
}
}
boolean find_match( template, inp ) {
This is where I need the magic to work out if the current element
in template matches the current position of inp, or inp+1, give or take.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CharBuffer
有一个内部索引,您可以从中进行相对读取,所以是的,这应该符合您的标准。您使用(继承的)
Buffer.position(int newPosition)
方法。您还可以
标记
一个位置,然后重置
到上一个标记。由于 NIO 包中的类通常用于(和用于)I/O,我鼓励您考虑包装一个
char[]
和一个int pos
课堂上:CharBuffer
has an internal index from which you can do relative reads from, so yes, that should fit your criterias.You set the position using the (inherited)
Buffer.position(int newPosition)
method.You can also
mark
a position, andreset
to the previous mark.Since classes from the NIO package usually are used for (and ment to be used for) I/O, I encourage you to consider wrapping up a
char[]
and anint pos
in class:您可以使用 Buffer.charAt(int) 来实现此操作,以获取相对于当前位置的字符,并使用 Buffer.get() 来前进当前位置。或者您可以使用 Buffer 操作来操纵位置。
但我认为你最好实现自己的类,将字符读入 char[] (或
String
),并提供执行基本操作的方法你需要。您可能使用CharBuffer
获得性能更高的解决方案,但这很可能不会成为性能瓶颈。Well you could implement this using
Buffer.charAt(int)
to get characters relative the current position, andBuffer.get()
to advance the current position. Or you could use theBuffer
operations for manipulating the position.But I think you'd be better of implementing your own class that reads the characters into an
char[]
(or aString
) and provides methods to perform the primitive operations that you need. You may get a more performant solution usingCharBuffer
, but the chances are that this won't be the performance bottleneck.