仿照JDK的String类作的D的String 类

[code]

//module jdk2d.lang;

import std.stdio;
/*
* The <code>String</code> class represents character strings. All
* string literals in D programs, such as <code>"abc"</code>, are
* implemented as instances of this class.
* @author  Caoqi
* @version 0.001, 07/03/30
* @since   JDK2D 0.01
* <p>
*/

public class  String{
  private final wchar[] value;
  
  /** The offset is the first index of the storage that is used. */
  private final int offset;
  
  /** The count is the number of characters in the String. */
  private final int count;   
  
  /*
   * 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 = new wchar[0];
  }

  /*
     * 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 count;
    }

    /**
     * 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>
     *
     * @since JDK2D 0.01
     */
    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>.
     *
     * @param   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);
    }
   
    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 &gt;= 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);
    }
   
    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.
     *
     * @param   source       the characters being searched.
     * @param   sourceOffset offset of the source string.
     * @param   sourceCount  count of the source string.
     * @param   target       the characters being searched for.
     * @param   targetOffset offset of the target string.
     * @param   targetCount  count of the target string.
     * @param   fromIndex    the index to begin searching from.
     */
    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;
      }
  }

public static void  main() {    
   String str = new String("The quick brown fox jumped over the lazy dog.");
   printf("%d\n",str.length());
   printf("%d\n",str.indexOf("z"));
   printf("%d\n",str.isEmpty());
}

[/code]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现的字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值