Module: LinuxStat::Misc

Defined in:
ext/misc/integer/integer?.c

Class Method Summary collapse

Class Method Details

.integer?(val) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'ext/misc/integer/integer?.c', line 28

VALUE isNumber(volatile VALUE obj, volatile VALUE val) {
  // But we don't expect anything other than String though as Argument.
  // Note that raising ArgumentError or any kind of Error shouldn't be done here
  // Otherwise Integer(n) is the best method in Ruby.
  if (!RB_TYPE_P(val, T_STRING))
    return Qnil ;

  char *str = StringValuePtr(val) ;
  char ch = str[0] ;

  // If the string is empty, return false
  if (!ch) return Qfalse ;

  unsigned char i = ch == '-' ? 1 : 0 ;
  if (!str[i]) return Qfalse ;

  unsigned char max = UCHAR_MAX ;

  # pragma GCC unroll 4
  while((ch = str[i++])) {
    if (ch < 48 || ch > 57)
      return Qfalse ;

    if (i == max)
      return Qnil ;
  }

  return Qtrue ;
}