如何让AttributedCharacterIterator返回给定属性的运行?

时间:2022-11-26 23:44:24

Suppose you assign a custom CharacterIterator.Attribute to the first five characters of a ten-character string.

假设您将自定义CharacterIterator.Attribute分配给十个字符的字符串的前五个字符。

Suppose further you assign a different CharacterIterator.Attribute to the remaining characters.

假设您为其余字符分配了不同的CharacterIterator.Attribute。

Why then, when I call AttributedString.getRunStart(firstAttribute) do I get 0 (I expect this) and when I call AttributedString.getRunStart(secondAttribute) do I also get 0?

为什么然后,当我调用AttributedString.getRunStart(firstAttribute)时,我得到0(我期待这个),当我调用AttributedString.getRunStart(secondAttribute)时,我也得到0吗?

Here's my setup code:

这是我的设置代码:

final AttributedString s = new AttributedString("SQ3R9FFFFF");
final Attribute baseID = new Attribute("Base ID") {};
final Attribute fs = new Attribute("FFF") {};
s.addAttribute(baseID, "Ignored", 0, 5);
s.addAttribute(fs, "Whatever", 5, 10);
final AttributedCharacterIterator iterator = s.getIterator();
assertNotNull(iterator);

And now here's some code that outputs some diagnostics:

现在这里有一些输出一些诊断的代码:

for (char c = iterator.first(); c != DONE; c = iterator.next()) {
  System.out.println("Character: " + c);
  System.out.println("Character index: " + iterator.getIndex());
  System.out.println("Attributes: " + iterator.getAttributes());
  System.out.println("Start for baseID: " + iterator.getRunStart(baseID));
  System.out.println("Limit for baseID: " + iterator.getRunLimit(baseID));
  System.out.println("Start for fs: " + iterator.getRunStart(fs));
  System.out.println("Limit for fs: " + iterator.getRunLimit(fs));
}

The output is this:

输出是这样的:

 Character: S
 Character index: 0
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: Q
 Character index: 1
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: 3
 Character index: 2
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: R
 Character index: 3
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: 9
 Character index: 4
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$1(Base ID)=Ignored}
 Start for baseID: 0
 Limit for baseID: 5
 Start for fs: 0
 Limit for fs: 5
 Character: F
 Character index: 5
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 6
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 7
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 8
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10
 Character: F
 Character index: 9
 Attributes: {com.foobar.collection.api.TestCaseAttributedString$2(FFF)=Whatever}
 Start for baseID: 5
 Limit for baseID: 10
 Start for fs: 5
 Limit for fs: 10

Note, in particular, the last entry, which reports that the "Start" for "baseID" is 5. Huh?

请注意,特别是最后一个条目,它报告“baseID”的“开始”是5.嗯?

1 个解决方案

#1


0  

Per the Javadoc:

根据Javadoc:

A run with respect to an attribute is a maximum text range for which:

相对于属性的运行是最大文本范围,其中:

  • the attribute is undefined or null for the entire range, or
  • 属性未定义或整个范围为null,或

  • the attribute value is defined and has the same non-null value for the entire range.
  • 属性值已定义,并且对整个范围具有相同的非空值。

I think it may be the first bullet point. For characters 0-4, the fs attribute is undefined, so it's a valid range. baseID is defined, and also valid.

我认为这可能是第一个要点。对于字符0-4,fs属性未定义,因此它是有效范围。 baseID已定义,也有效。

http://download.oracle.com/javase/1,5.0/docs/api/java/text/AttributedCharacterIterator.html

#1


0  

Per the Javadoc:

根据Javadoc:

A run with respect to an attribute is a maximum text range for which:

相对于属性的运行是最大文本范围,其中:

  • the attribute is undefined or null for the entire range, or
  • 属性未定义或整个范围为null,或

  • the attribute value is defined and has the same non-null value for the entire range.
  • 属性值已定义,并且对整个范围具有相同的非空值。

I think it may be the first bullet point. For characters 0-4, the fs attribute is undefined, so it's a valid range. baseID is defined, and also valid.

我认为这可能是第一个要点。对于字符0-4,fs属性未定义,因此它是有效范围。 baseID已定义,也有效。

http://download.oracle.com/javase/1,5.0/docs/api/java/text/AttributedCharacterIterator.html