| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| STATIC I32 is_num(pTHX_ SV * sv); |
| 8 |
52 |
| STATIC I32 is_num(pTHX_ SV * sv) { |
| 9 |
52 |
| I32 type = 0; |
| 10 |
|
| |
| 11 |
52 |
▶ | if (sv && SvOK(sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sv && SvOK(sv)) { |
52 |
0 |
52 |
| if (sv && SvOK(sv)) { |
51 |
1 |
52 |
|
| 12 |
|
| STRLEN len; |
| 13 |
51 |
| const char * str = NULL; |
| 14 |
|
| |
| 15 |
51 |
▶ | if (SvPOK(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvPOK(sv)) { |
21 |
30 |
51 |
|
| 16 |
21 |
| str = SvPVX_const(sv); |
| 17 |
21 |
| len = SvCUR(sv); |
| 18 |
|
| } else { /* stringify numbers, references and overloaded objects */ |
| 19 |
30 |
| str = SvPV_const(sv, len); |
| 20 |
|
| } |
| 21 |
|
| |
| 22 |
|
| /* |
| 23 |
|
| * handle 1.#INF (Inf), -1.#INF (-Inf), and 1.#IND (NaN) on Windows: |
| 24 |
|
| * http://www.johndcook.com/IEEE_exceptions_in_cpp.html |
| 25 |
|
| * https://rt.perl.org/Public/Bug/Display.html?id=122843 |
| 26 |
|
| * |
| 27 |
|
| * switch-trie generated by Devel::Tokenizer::C (Marcus Holland-Moritz++) |
| 28 |
|
| * hacked about to make the formatting less annoying :-) |
| 29 |
|
| */ |
| 30 |
|
| |
| 31 |
|
| #ifdef WIN32 |
| 32 |
|
| switch (len) { |
| 33 |
|
| case 6: /* 2 tokens of length 6 */ |
| 34 |
|
| switch (str[5]) { |
| 35 |
|
| case 'D': |
| 36 |
|
| case 'd': |
| 37 |
|
| if (str[0] == '1' && |
| 38 |
|
| str[1] == '.' && |
| 39 |
|
| str[2] == '#' && |
| 40 |
|
| (str[3] == 'I' || str[3] == 'i') && |
| 41 |
|
| (str[4] == 'N' || str[4] == 'n')) |
| 42 |
|
| { /* 1.#IND */ |
| 43 |
|
| return IS_NUMBER_NAN | IS_NUMBER_NOT_INT; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| goto fail_early; |
| 47 |
|
| |
| 48 |
|
| case 'F': |
| 49 |
|
| case 'f': |
| 50 |
|
| if (str[0] == '1' && |
| 51 |
|
| str[1] == '.' && |
| 52 |
|
| str[2] == '#' && |
| 53 |
|
| (str[3] == 'I' || str[3] == 'i') && |
| 54 |
|
| (str[4] == 'N' || str[4] == 'n')) |
| 55 |
|
| { /* 1.#INF */ |
| 56 |
|
| return IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| goto fail_early; |
| 60 |
|
| |
| 61 |
|
| default: |
| 62 |
|
| goto not_nan_or_inf; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| case 7: /* 2 tokens of length 7 */ |
| 66 |
|
| switch (str[6]) { |
| 67 |
|
| case 'D': |
| 68 |
|
| case 'd': |
| 69 |
|
| if (str[0] == '-' && |
| 70 |
|
| str[1] == '1' && |
| 71 |
|
| str[2] == '.' && |
| 72 |
|
| str[3] == '#' && |
| 73 |
|
| (str[4] == 'I' || str[4] == 'i') && |
| 74 |
|
| (str[5] == 'N' || str[5] == 'n')) |
| 75 |
|
| { /* -1.#IND */ |
| 76 |
|
| return IS_NUMBER_NEG | IS_NUMBER_NAN | IS_NUMBER_NOT_INT; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| goto fail_early; |
| 80 |
|
| |
| 81 |
|
| case 'F': |
| 82 |
|
| case 'f': |
| 83 |
|
| if (str[0] == '-' && |
| 84 |
|
| str[1] == '1' && |
| 85 |
|
| str[2] == '.' && |
| 86 |
|
| str[3] == '#' && |
| 87 |
|
| (str[4] == 'I' || str[4] == 'i') && |
| 88 |
|
| (str[5] == 'N' || str[5] == 'n')) |
| 89 |
|
| { /* -1.#INF */ |
| 90 |
|
| return IS_NUMBER_NEG | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| goto fail_early; |
| 94 |
|
| |
| 95 |
|
| default: |
| 96 |
|
| goto not_nan_or_inf; |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
|
| default: |
| 100 |
|
| goto not_nan_or_inf; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| fail_early: |
| 104 |
|
| return 0; |
| 105 |
|
| |
| 106 |
|
| not_nan_or_inf: |
| 107 |
|
| #endif |
| 108 |
51 |
| type = grok_number(str, len, NULL); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
52 |
| return type; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| MODULE = Scalar::Util::Numeric PACKAGE = Scalar::Util::Numeric |
| 115 |
|
| |
| 116 |
|
| void |
| 117 |
|
| uvmax() |
| 118 |
|
| PROTOTYPE: |
| 119 |
|
| CODE: |
| 120 |
1 |
| XSRETURN_UV(UV_MAX); |
| 121 |
|
| |
| 122 |
|
| void |
| 123 |
|
| isnum(sv) |
| 124 |
|
| SV * sv |
| 125 |
|
| PROTOTYPE: $ |
| 126 |
|
| CODE: |
| 127 |
13 |
| XSRETURN_IV(is_num(aTHX_ sv)); |
| 128 |
|
| |
| 129 |
|
| void |
| 130 |
|
| isint(sv) |
| 131 |
|
| SV * sv |
| 132 |
|
| PROTOTYPE: $ |
| 133 |
|
| CODE: |
| 134 |
13 |
| I32 type = is_num(aTHX_ sv); |
| 135 |
|
| I32 ret; |
| 136 |
|
| |
| 137 |
13 |
▶ | if (type == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type == 1) { |
3 |
10 |
13 |
|
| 138 |
3 |
| ret = 1; |
| 139 |
10 |
▶ | } else if (type == 9) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (type == 9) { |
1 |
9 |
10 |
|
| 140 |
1 |
| ret = -1; |
| 141 |
|
| } else { |
| 142 |
9 |
| ret = -0; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
13 |
| XSRETURN_IV(ret); |
| 146 |
|
| |
| 147 |
|
| void |
| 148 |
|
| isuv(sv) |
| 149 |
|
| SV * sv |
| 150 |
|
| PROTOTYPE: $ |
| 151 |
|
| CODE: |
| 152 |
2 |
| XSRETURN_IV((is_num(aTHX_ sv) & 1) ? 1 : 0); |
| 153 |
|
| |
| 154 |
|
| void |
| 155 |
|
| isbig(sv) |
| 156 |
|
| SV * sv |
| 157 |
|
| PROTOTYPE: $ |
| 158 |
|
| CODE: |
| 159 |
2 |
| XSRETURN_IV((is_num(aTHX_ sv) & 2) ? 1 : 0); |
| 160 |
|
| |
| 161 |
|
| void |
| 162 |
|
| isfloat(sv) |
| 163 |
|
| SV * sv |
| 164 |
|
| PROTOTYPE: $ |
| 165 |
|
| CODE: |
| 166 |
5 |
| XSRETURN_IV((is_num(aTHX_ sv) & 4) ? 1 : 0); |
| 167 |
|
| |
| 168 |
|
| void |
| 169 |
|
| isneg(sv) |
| 170 |
|
| SV * sv |
| 171 |
|
| PROTOTYPE: $ |
| 172 |
|
| CODE: |
| 173 |
4 |
| XSRETURN_IV((is_num(aTHX_ sv) & 8) ? 1 : 0); |
| 174 |
|
| |
| 175 |
|
| void |
| 176 |
|
| isinf(sv) |
| 177 |
|
| SV * sv |
| 178 |
|
| PROTOTYPE: $ |
| 179 |
|
| CODE: |
| 180 |
7 |
| XSRETURN_IV((is_num(aTHX_ sv) & 16) ? 1 : 0); |
| 181 |
|
| |
| 182 |
|
| void |
| 183 |
|
| isnan(sv) |
| 184 |
|
| SV * sv |
| 185 |
|
| PROTOTYPE: $ |
| 186 |
|
| CODE: |
| 187 |
6 |
| XSRETURN_IV((is_num(aTHX_ sv) & 32) ? 1 : 0); |