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).

1  
sclass Substring implements /*Comparable<? extends CharSequence>,*/ CharSequence {
2  
  CharSequence s; // full string we reference
3  
  int i, l; // starting index, length
4  
  
5  
  *() {}
6  
  *(CharSequence *s, int *i, int j) { l = j-i; }
7  
  *(CharSequence *s, int *i) { l = s.length()-i; }
8  
  
9  
  public bool equals(O o) {
10  
    if (o cast CharSequence) {
11  
      CharSequence c = o;
12  
      if (l != c.length()) false;
13  
      for idx to l:
14  
        if (s.charAt(i+idx) != c.charAt(idx))
15  
          false;
16  
      true;
17  
    }
18  
    false;
19  
  }
20  
  
21  
  toString {
22  
    ret charSequenceToStringByChar(this);
23  
  }
24  
25  
  public char charAt(int index) {
26  
    if (index < 0 || index >= l) fail("Index out of range: " + index + " (length: " + l + ")");
27  
    ret s.charAt(i+index);
28  
  }
29  
  
30  
  public int length() { ret l; }
31  
  public int startIndex() { ret i; }
32  
  public int endIndex() { ret i+l; }
33  
  public CharSequence baseString() { ret s; }
34  
  
35  
  IntRange range() { ret IntRange(startIndex(), endIndex()); }
36  
  
37  
  public CharSequence subSequence(int start, int end) {
38  
    ret substring(start, end);
39  
  }
40  
  
41  
  // TODO: index checking
42  
  Substring substring(int start, int end) {
43  
    ret Substring(s, i+start, i+end);
44  
  }
45  
  
46  
  Substring substring(int start) {
47  
    ret substring(start, l);
48  
  }
49  
  
50  
  bool endsAtEnd() { ret i+l == s.length(); }
51  
}

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: 292 / 681
Version history: 17 change(s)
Referenced in: [show references]