Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1028129 // Substring - compactly stored substring

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2657L/17K).

sclass Substring implements /*Comparable<? extends CharSequence>,*/ CharSequence {
  CharSequence s; // full string we reference
  int i, l; // starting index, length
  
  *() {}
  *(CharSequence *s, int *i, int j) { l = j-i; }
  *(CharSequence *s, int *i) { l = s.length()-i; }
  
  public bool equals(O o) {
    if (o cast CharSequence) {
      CharSequence c = o;
      if (l != c.length()) false;
      for idx to l:
        if (s.charAt(i+idx) != c.charAt(idx))
          false;
      true;
    }
    false;
  }
  
  toString {
    ret charSequenceToStringByChar(this);
  }

  public char charAt(int index) {
    if (index < 0 || index >= l) fail("Index out of range: " + index + " (length: " + l + ")");
    ret s.charAt(i+index);
  }
  
  public int length() { ret l; }
  public int startIndex() { ret i; }
  public int endIndex() { ret i+l; }
  public CharSequence baseString() { ret s; }
  
  IntRange range() { ret IntRange(startIndex(), endIndex()); }
  
  public CharSequence subSequence(int start, int end) {
    ret substring(start, end);
  }
  
  // TODO: index checking
  Substring substring(int start, int end) {
    ret Substring(s, i+start, i+end);
  }
  
  Substring substring(int start) {
    ret substring(start, l);
  }
  
  bool endsAtEnd() { ret i+l == s.length(); }
}

Author comment

Began life as a copy of #1002873

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028129
Snippet name: Substring - compactly stored substring
Eternal ID of this version: #1028129/18
Text MD5: 7cfab025e773b6c3c90c5c771d847d00
Transpilation MD5: 3d3bd65aff2334bb64ca3e8b9f28c051
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-27 18:30:51
Source code size: 1343 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 234 / 604
Version history: 17 change(s)
Referenced in: [show references]