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

34
LINES

< > BotCompany Repo | #1029161 // compareCharIterators - compare strings represented by char iterators lexicographically

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

Libraryless. Click here for Pure Java version (66L/1K).

static int compareCharIterators(Iterator<Char> it1, Iterator<Char> it2) {
  while true {
    if (!it1.hasNext())
      ret it2.hasNext() ? -1 : 0;
    if (!it2.hasNext()) ret 1;
    char a = it1.next(), b = it2.next();
    if (a < b) ret -1;
    if (a > b) ret 1;
  }
}

static int compareCharIterators(CharacterIterator it1, CharacterIterator it2) {
  while true {
    if (!it1.hasNext())
      ret it2.hasNext() ? -1 : 0;
    if (!it2.hasNext()) ret 1;
    char a = it1.next(), b = it2.next();
    if (a < b) ret -1;
    if (a > b) ret 1;
  }
}

static int compareCharIterators(CharacterIterator it1, CharacterIterator it2, CharComparator comparator) {
  if (comparator == null) ret compareCharIterators(it1, it2);
  
  while true {
    if (!it1.hasNext())
      ret it2.hasNext() ? -1 : 0;
    if (!it2.hasNext()) ret 1;
    char a = it1.next(), b = it2.next();
    int cmp = comparator.compare(a, b);
    if (cmp != 0) ret cmp;
  }
}

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: #1029161
Snippet name: compareCharIterators - compare strings represented by char iterators lexicographically
Eternal ID of this version: #1029161/8
Text MD5: 9d6a86f157f8671166ffae9196fd54af
Transpilation MD5: a1e2fed74cfba29640411e23e3b627c6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-23 09:27:22
Source code size: 970 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 171 / 256
Version history: 7 change(s)
Referenced in: [show references]