File: Util2.cpp

package info (click to toggle)
pymol 1.8.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,248 kB
  • ctags: 24,095
  • sloc: cpp: 474,635; python: 75,034; ansic: 22,888; sh: 236; makefile: 78; csh: 21
file content (81 lines) | stat: -rw-r--r-- 1,444 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Utility functions
 *
 * (c) 2015 Schrodinger, Inc.
 */

#include <cstdio>
#include <cctype>
#include <sstream>
#include <string>
#include <vector>

#include "Util2.h"

/*
 * strsplit: split string `s` by character `delim`
 *
 * If `delim` is null, then do whitespace split.
 */
std::vector<std::string> strsplit(const std::string &s, char delim) {
  std::vector<std::string> elems;
  std::istringstream iss(s);
  std::string item;

  if (delim) {
    // token split
    while (std::getline(iss, item, delim))
      elems.push_back(item);
  } else {
    // whitespace split
    while (iss >> item)
      elems.push_back(item);
  }

  return elems;
}

/*
 * Natural string compare: F1 < F2 < F10
 *
 * Return true if a < b
 */
bool cstrlessnat(const char * a, const char * b) {
  if (!b[0])
    return false;
  if (!a[0])
    return true;

  bool a_digit = isdigit(a[0]);
  bool b_digit = isdigit(b[0]);

  if (a_digit && !b_digit)
    return true;
  if (!a_digit && b_digit)
    return false;

  if (!a_digit && !b_digit) {
    if (a[0] != b[0])
      return (a[0] < b[0]);

    return cstrlessnat(a + 1, b + 1);
  }

  int ia, ib, na, nb;
  sscanf(a, "%d%n", &ia, &na);
  sscanf(b, "%d%n", &ib, &nb);

  if (ia != ib)
    return ia < ib;

  return cstrlessnat(a + na, b + nb);
}

/*
 * Natural string compare: F1 < F2 < F10
 */
bool strlessnat(const std::string& a, const std::string& b) {
  return cstrlessnat(a.c_str(), b.c_str());
}