A new class -->String 0.021
jinheking
caoqi at bgi.net.cn
Thu Apr 5 00:54:22 PDT 2007
//module jdk2d.lang;
import std.stdio;
import std.string;
/**
* The <code>String</code> class represents character strings. All
* string literals in D programs, such as <code>String str=new
String("abc");</code>,
* <br /><code>str="Hello world";</code>
* are implemented as instances of this class.<br />
* final wchar[] value; The value is the String's value.<br />
* final int offset; The offset is the first index of the storage that is
used.<br />
* final int count; The count is the number of characters in the String.
<br />
* Authors: Caoqi
* version: 0.021
* Date: April 5, 2007
* History: March 30,2007 V0.01
* License: BSD
* See_Also:
My blog, <a href=" http://jinheking.javaeye.com/">
http://jinheking.javaeye.com/</a>
* <p>
*/
/// Documentation for String
public class String{
private:
final wchar[] value; ///The value is the String's value.
final int offset; ///The offset is the first index of the storage that
is used.
final int count; ///The count is the number of characters in the
String.
public:
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
this(){
this.offset = 0;
this.count = 0;
this.value = null;
}
/**
* Params:wc = The initial value of the string
* Return:
* String
* <code>String str=new String("abc");</code><br />
* <code>str="abcd";</code>
*/
String opAssign(wchar[] wc) {
this.offset = 0;
this.count = wc.length;
this.value = wc.dup;
return this;
}
/**
* <code>s1>s2</code>
* Params: s = The initial value of the string
* Return: bool
*/
int opCmp(String s){
return std.string.cmp(cast(char[])this.value, cast(char[])s.value);
}
/*
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification
of
* the character array does not affect the newly created string.
*
* Param: value = The initial value of the string
*/
public this(wchar[] value) {
int size = value.length;
this.offset = 0;
this.count = size;
this.value = value;
}
/**
* Returns the length of this string.
* The length is equal to the number of <a
href="Character.html#unicode">Unicode
* code units</a> in the string.
*
* Return: The length of the sequence of characters represented by this
* object.
*/
public int length() {
return this.value.length;
}
/**
* Returns <tt>true</tt> if, and only if, {@link #length()} is
<tt>0</tt>.
*
* Return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
* <tt>false</tt>
*/
public bool isEmpty() {
return count == 0;
}
/**
* Returns the index within this string of the first occurrence of the
* specified substring. The integer returned is the smallest value
* <i>k</i> such that:
* <blockquote><pre>
* this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* is <code>true</code>.
*
* Params: str = any String.
* Return: if the string argument occurs as a substring within this
* object, then the index of the first character of the first
* such substring is returned; if it does not occur as a
* substring, <code>-1</code> is returned.
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* Returns the index within this string of the first occurrence of the
* specified substring. The integer returned is the smallest value
* <i>k</i> such that:
* <blockquote><pre>
* this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* is <code>true</code>.
*
* Params: str = any String.
* Return: if the string argument occurs as a substring within this
* object, then the index of the first character of the first
* such substring is returned; if it does not occur as a
* substring, <code>-1</code> is returned.
*/
public int indexOf(wchar[] str) {
return indexOf(str, 0);
}
/**
* Returns the index within this string of the first occurrence of the
* specified substring, starting at the specified index. The integer
* returned is the smallest value <tt>k</tt> for which:
* <blockquote><pre>
* k >= Math.min(fromIndex, this.length()) &&
this.startsWith(str, k)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then -1 is returned.
*
* Param: str = the substring for which to search.
* Param: fromIndex = the index from which to start the search.
* Return: the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, offset, count,
str.value, str.offset, str.count, fromIndex);
}
/**
* Returns the index within this string of the first occurrence of the
* specified substring, starting at the specified index. The integer
* returned is the smallest value <tt>k</tt> for which:
* <blockquote><pre>
* k >= Math.min(fromIndex, this.length()) &&
this.startsWith(str, k)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then -1 is returned.
*
* Param: str = the substring for which to search.
* Param: fromIndex = the index from which to start the search.
* Return: the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*/
public int indexOf(wchar[] wstr, int fromIndex) {
String str=new String(wstr);
return indexOf(value, offset, count,
str.value, str.offset, str.count, fromIndex);
}
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* Params: source = the characters being searched.
* Params: sourceOffset= offset of the source string.
* Params: sourceCount= count of the source string.
* Params: target = the characters being searched for.
* Params: targetOffset =offset of the target string.
* Params: targetCount = count of the target string.
* Params: fromIndex = the index to begin searching from.
* Return: the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*/
static int indexOf(wchar[] source, int sourceOffset, int sourceCount,
wchar[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
wchar first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
i++;
while (i <= max && source[i] != first){
i++;
}
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && (source[j] ==
target[k]);j++){
k++;
}
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
/**
* Return:The String's value
*/
char[] toString(){
return cast(char[])std.utf.toUTF8(this.value);
}
}
/++++++++++++++++++++++++
+ Our function.
+ Example:
+ --------------------------
+ import std.stdio;
+
+ void foo()
+ {
+ String str=new String("Hi,jdk2d String,I need you!") ;
+ writefln(str); /* print the string */
+ str="Next,I calculate the str's length"
+ writefln(str); /* print the string */
+ printf("%d\n",str.length());
+ }
+ --------------------------
+/
public static void main() {
String str = new String("The quick brown fox jumped over the lazy dog.");
String s1 = new String("abc");
s1="abc";
String s2=new String("ab");
writef("s1=");
writefln(s1);
writef("s2=");
writefln(s2);
if(s1>s2)
printf("s1>s2=%.*s\n","true");
else
printf("s1>s2=%.*s\n","false");
String s3=new String("ab");
String s4=new String("abc");
writef("s3=");
writefln(s3);
writef("s4=");
writefln(s4);
if(s3>s4)
printf("s3>s4=%.*s\n","true");
else
printf("s3>s4=%.*s\n","false");
printf("%d\n",str.indexOf("z"));
printf("%d\n",str.isEmpty());
}
-----------------------------------------------------------------------------------
String.ddoc
B = <b>$0</b>
I = <i>$0</i>
U = <u>$0</u>
P = <p>$0</p>
DL = <dl>$0</dl>
DT = <dt>$0</dt>
DD = <dd>$0</dd>
TABLE = <table>$0</table>
TR = <tr>$0</tr>
TH = <th>$0</th>
TD = <td>$0</td>
OL = <ol>$0</ol>
UL = <ul>$0</ul>
LI = <li>$0</li>
BIG = <big>$0</big>
SMALL = <small>$0</small>
BR = <br>
LINK = <a href="$0">$0</a>
LINK2 = <a href="$1">$+</a>
RED = <font color=red>$0</font>
BLUE = <font color=blue>$0</font>
GREEN = <font color=green>$0</font>
YELLOW =<font color=yellow>$0</font>
BLACK = <font color=black>$0</font>
WHITE = <font color=white>$0</font>
D_CODE = <pre class="d_code">$0</pre>
D_COMMENT = $(GREEN $0)
D_STRING = $(RED $0)
D_KEYWORD = $(BLUE $0)
D_PSYMBOL = $(U $0)
D_PARAM = $(I $0)
DDOC = <html><head>
<META http-equiv="content-type" content="text/html; charset=utf-8">
<title>$(TITLE)</title>
</head><body>
<h1>$(TITLE)</h1>
$(BODY)
</body></html>
DDOC_COMMENT = <!-- $0 -->
DDOC_DECL = $(DT $(BIG $0))
DDOC_DECL_DD = $(DD $0)
DDOC_DITTO = $(BR)$0
DDOC_SECTIONS = $0
DDOC_SUMMARY = $0$(BR)$(BR)
DDOC_DESCRIPTION = $0$(BR)$(BR)
DDOC_AUTHORS = $(B Authors:)$(BR)
$0$(BR)$(BR)
DDOC_BUGS = $(RED BUGS:)$(BR)
$0$(BR)$(BR)
DDOC_COPYRIGHT = $(B Copyright:)$(BR)
$0$(BR)$(BR)
DDOC_DATE = $(B Date:)$(BR)
$0$(BR)$(BR)
DDOC_DEPRECATED = $(RED Deprecated:)$(BR)
$0$(BR)$(BR)
DDOC_EXAMPLES = $(B Examples:)$(BR)
$0$(BR)$(BR)
DDOC_HISTORY = $(B History:)$(BR)
$0$(BR)$(BR)
DDOC_LICENSE = $(B License:)$(BR)
$0$(BR)$(BR)
DDOC_RETURNS = $(B Returns:)$(BR)
$0$(BR)$(BR)
DDOC_SEE_ALSO = $(B See Also:)$(BR)
$0$(BR)$(BR)
DDOC_STANDARDS = $(B Standards:)$(BR)
$0$(BR)$(BR)
DDOC_THROWS = $(B Throws:)$(BR)
$0$(BR)$(BR)
DDOC_VERSION = $(B Version:)$(BR)
$0$(BR)$(BR)
DDOC_SECTION_H = $(B $0)$(BR)$(BR)
DDOC_SECTION = $0$(BR)$(BR)
DDOC_MEMBERS = $(DL $0)
DDOC_MODULE_MEMBERS = $(DDOC_MEMBERS $0)
DDOC_CLASS_MEMBERS = $(DDOC_MEMBERS $0)
DDOC_STRUCT_MEMBERS = $(DDOC_MEMBERS $0)
DDOC_ENUM_MEMBERS = $(DDOC_MEMBERS $0)
DDOC_TEMPLATE_MEMBERS = $(DDOC_MEMBERS $0)
DDOC_PARAMS = $(B Params:)$(BR)\n$(TABLE $0)$(BR)
DDOC_PARAM_ROW = $(TR $0)
DDOC_PARAM_ID = $(TD $0)
DDOC_PARAM_DESC = $(TD $0)
DDOC_BLANKLINE = $(BR)$(BR)
DDOC_PSYMBOL = $(U $0)
DDOC_KEYWORD = $(B $0)
DDOC_PARAM = $(I $0)
------------------------------------------------------------------------------------
dmd -D String.d String.ddoc
More information about the Digitalmars-d
mailing list