RE: [Ikvm-developers] (no subject)
Brought to you by:
jfrijters
|
From: Jonathan P. <jp...@ny...> - 2003-07-25 14:18:45
|
Jeroen,
>the calling code expects an interface CharSequence. I can't just
>substitute a string for that because the same code might also want work
>with other classes that implement CharSequence.
I finally understand now, thanks for explaining it so well.
>BTW, it would be possible for me to implement String.subSequence, by
>returning an instance of my own helper class that implements
>CharSequence, but since this is only a partial (and probably useless)
>solution, I chose not to do that.
Here's some code that I wrote that you may want to use seems to work for me
that implements CharSequence as an interface. It works as you suggest,
returning a concrete implementation of the CharSequence interface.
-Jonathan
In StringHelper.cs:
public static object subSequence(string s, int offset, int count)
{
// throw new NotImplementedException();
return (new StringWrapper (s, offset, count));
}
New File: CharSequence.cs added to IK.VM.NET
using System;
interface CharSequence
{
/**
* Returns the length of this character sequence. The length is the
number
* of 16-bit Unicode characters in the sequence. </p>
*
* @return the number of characters in this sequence
*/
int length();
/**
* Returns the character at the specified index. An index ranges from
zero
* to <tt>length() - 1</tt>. The first character of the sequence is at
* index zero, the next at index one, and so on, as for array
* indexing. </p>
*
* @param index the index of the character to be returned
*
* @return the specified character
*
* @throws IndexOutOfBoundsException
* if the <tt>index</tt> argument is negative or not less than
* <tt>length()</tt>
*/
char charAt(int index);
/**
* Returns a new character sequence that is a subsequence of this
sequence.
* The subsequence starts with the character at the specified index and
* ends with the character at index <tt>end - 1</tt>. The length of the
* returned sequence is <tt>end - start</tt>, so if <tt>start ==
end</tt>
* then an empty sequence is returned. </p>
*
* @param start the start index, inclusive
* @param end the end index, exclusive
*
* @return the specified subsequence
*
* @throws IndexOutOfBoundsException
* if <tt>start</tt> or <tt>end</tt> are negative,
* if <tt>end</tt> is greater than <tt>length()</tt>,
* or if <tt>start</tt> is greater than <tt>end</tt>
*/
CharSequence subSequence(int start, int end);
/**
* Returns a string containing the characters in this sequence in the
same
* order as this sequence. The length of the string will be the length
of
* this sequence. </p>
*
* @return a string consisting of exactly this sequence of characters
*/
string toString();
}
New File: StringWrapper.cs
using System;
public class StringWrapper : CharSequence
{
private String fData = "";
private int fOffset = 0;
private int fLength = 0;
// length of sequence
public int length ()
{
return fLength;
}
// character at a given index
public char charAt (int index)
{
if (index < 0 || index >= fLength)
{
throw new System.Exception
("IndexOutOfBoundsException invalid index");
}
return (fData [index + fOffset]);
}
// subsequence from start (inclusive) to end (exclusive)
CharSequence CharSequence.subSequence (int start, int end)
{
return (this.subSequence (start, end));
}
public StringWrapper subSequence (int start, int end)
{
if (start < 0 || end < 0 || end > fLength || start > end)
{
throw new System.Exception ("
IndexOutOfBoundsException invalid start/end");
}
return new StringWrapper (fData, start + fOffset, end -
start);
}
// convert to string
public String toString ()
{
return (this.fData);
}
// construct an StringWrapper from a portion of a String
public StringWrapper (String theString, int itsOffset, int
itsLength)
{
if (theString == null)
{
throw new System.Exception ("NullPointerException
null String");
}
if ( (fOffset < 0) || (fLength < 0) || (fOffset >
(theString.Length - fLength)))
{
throw new System.Exception
("IllegalArgumentException bad Offset / Length");
}
this.fData = theString;
this.fOffset = itsOffset;
this.fLength = itsLength;
}
// construct a StringWrapper from a char array
public StringWrapper (String itsString)
: this (itsString, 0, itsString.Length)
{
}
}
|