1999-04-07 22:42:40 +08:00
|
|
|
// StringCharacterIterator.java - Iterate over string of Unicode characters.
|
|
|
|
|
2000-05-04 23:50:34 +08:00
|
|
|
/* Copyright (C) 1999, 2000 Free Software Foundation
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
package java.text;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tom Tromey <tromey@cygnus.com>
|
|
|
|
* @date February 22, 1999
|
|
|
|
*/
|
|
|
|
/* Written using "Java Class Libraries", 2nd edition, plus online
|
|
|
|
* API docs for JDK 1.2 beta from http://www.javasoft.com.
|
|
|
|
* Status: Believed complete and correct to 1.1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public final class StringCharacterIterator implements CharacterIterator
|
|
|
|
{
|
|
|
|
public Object clone ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
return (Object) new StringCharacterIterator (text, begin, end, pos);
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public char current ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
// This follows JDK 1.2 semantics and not 1.1 semantics.
|
|
|
|
// In 1.1 we would throw an exception if begin==end.
|
|
|
|
return (pos < end) ? text.charAt(pos) : CharacterIterator.DONE;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public boolean equals (Object obj)
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
if (! (obj instanceof StringCharacterIterator))
|
|
|
|
return false;
|
|
|
|
StringCharacterIterator sci = (StringCharacterIterator) obj;
|
|
|
|
// The spec says "the same text". We take this to mean equals,
|
|
|
|
// not ==.
|
|
|
|
return (pos == sci.pos
|
|
|
|
&& begin == sci.begin
|
|
|
|
&& end == sci.end
|
|
|
|
&& text.equals(sci.text));
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public char first ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
pos = begin;
|
|
|
|
return current ();
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public int getBeginIndex ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
return begin;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public int getEndIndex ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
return end;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public int getIndex ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
return pos;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public int hashCode ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
// FIXME: this is a terrible hash code. Find a better one.
|
|
|
|
return text.hashCode() + pos + begin + end;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public char last ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
pos = end;
|
|
|
|
return current ();
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public char next ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
if (pos == end)
|
|
|
|
return CharacterIterator.DONE;
|
|
|
|
++pos;
|
|
|
|
return current ();
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public char previous ()
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
if (pos == begin)
|
|
|
|
return CharacterIterator.DONE;
|
|
|
|
--pos;
|
|
|
|
return current ();
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public char setIndex (int idx)
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
// In 1.1 we would throw an error if `idx == end'.
|
|
|
|
if (idx < begin || idx > end)
|
|
|
|
throw new IllegalArgumentException ();
|
|
|
|
pos = idx;
|
|
|
|
return current ();
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
public StringCharacterIterator (String text)
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
this (text, 0, text.length(), 0);
|
|
|
|
}
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
public StringCharacterIterator (String text, int pos)
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
this (text, 0, text.length(), pos);
|
|
|
|
}
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
public StringCharacterIterator (String text, int begin, int end, int pos)
|
2000-11-18 10:29:13 +08:00
|
|
|
{
|
|
|
|
if (begin < 0 || begin > end || end > text.length()
|
|
|
|
// In 1.1 we would also throw if `pos == end'.
|
|
|
|
|| pos < begin || pos > end)
|
|
|
|
throw new IllegalArgumentException ();
|
|
|
|
|
|
|
|
this.text = text;
|
|
|
|
this.begin = begin;
|
|
|
|
this.end = end;
|
|
|
|
this.pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The online 1.2 docs say that this is "package visible" in the
|
|
|
|
// method description, but they also say it is public. We choose
|
|
|
|
// the latter for compatibility with the actual implementation.
|
|
|
|
public void setText (String text)
|
|
|
|
{
|
|
|
|
this.text = text;
|
|
|
|
this.begin = 0;
|
|
|
|
this.end = text.length ();
|
|
|
|
this.pos = 0;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
// String to iterate over.
|
|
|
|
private String text;
|
|
|
|
// Current position.
|
|
|
|
private int pos;
|
|
|
|
// Start position in string.
|
|
|
|
private int begin;
|
|
|
|
// End position in string.
|
|
|
|
private int end;
|
|
|
|
}
|