File: stringandnumericcomparison.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (60 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download | duplicates (3)
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
#include "stringandnumericcomparison.h"
using namespace std;


bool stringAndNumericLessThan(const string & a, const string & b)
{
  unsigned long aidx=0;
  unsigned long bidx=0;
  while ((aidx<a.size()) && (bidx<b.size()))
  {
    if ( (0!=isdigit(a[aidx])) && (0!=isdigit(b[bidx])) )
    {
      // fetch numbers
      long anumber=0;
      long bnumber=0;
      while ( (0!=isdigit(a[aidx])) && (aidx<a.size()) )
      {
        anumber*=10;
        anumber+=a[aidx]-'0';
        aidx++;
      }
      while ( (0!=isdigit(b[bidx])) && (bidx<b.size()) )
      {
        bnumber*=10;
        bnumber+=b[bidx]-'0';
        bidx++;
      }

      // the numeric comparison
      if (anumber<bnumber)
      {
        return true;
      }
      if (anumber>bnumber)
      {
        return false;
      }
    }
    else
    {
      // fetch character
      char achar=a[aidx++];
      char bchar=b[bidx++];

      // the character comparison
      if (achar<bchar)
      {
        return true;
      }
      if (achar>bchar)
      {
        return false;
      }
    }
  }
  // strings seem to be identical up to their common size
  // last criterion: size of strings
  return (a.size()<b.size());
}