int SerialPortOpenPortNamed(char *portName, int baudRate) { int portNum; int n = strlen(portName); if ((n < 4) || (n > 5)) return PRIM_FAILED; // assume portName is: COM<num>, where <num> is or two decimal digits if (digitValue(portName[3]) < 0) return PRIM_FAILED; portNum = digitValue(portName[3]); if (n == 5) { if (digitValue(portName[4]) < 0) return PRIM_FAILED; portNum = (10 * portNum) + digitValue(portName[4]); } return openPort(portNum, baudRate); }
int romanToNumber(char number[1000]) { int i = 0, sum = 0; while (number[i]) { if (digitValue(number[i]) >= digitValue(number[i + 1])) sum = sum + digitValue(number[i]); else { sum = sum + (digitValue(number[i + 1]) - digitValue(number[i])); i++; } i++; } return sum; }
T read(IStream& stream) const { Case streamCase = effectiveCase(stream); std::string input = stream.readToken(); bool is_signed = std::numeric_limits<T>::is_signed; if(!is_signed && input[0] == '-') stream.quit(Verdict::PE, expectation("Unsigned integer", input)); const char* usedValue = input.c_str(); size_t length = input.length(); bool negative = false; if(input[0] == '-'){ negative = true; ++usedValue; --length; } static const std::vector<int> maxArray = absToArray(std::numeric_limits<T>::max()); if(length > maxArray.size()) stream.quit(Verdict::PE, expectation("Integer", input)); static std::vector<int> digits(maxArray.size()); for(size_t i = 0; i < length; ++i){ try { digits[i] = digitValue(usedValue[i], streamCase); } catch(NotDigitException& e){ stream.quit(Verdict::PE, expectation("Digit", e.character)); } if(digits[i] >= radix) stream.quit(Verdict::PE, expectation("Digit in radix " + toString(radix), usedValue[i])); } static const std::vector<int> minArray = absToArray(std::numeric_limits<T>::min()); if(negative && digits == minArray){ return std::numeric_limits<T>::min(); } if(digits.empty()) stream.quit(Verdict::PE, expectation("Integer", input)); if(digits[0] == 0 && (negative || length > 1)) stream.quit(Verdict::PE, expectation("Integer", input)); if(length == maxArray.size() && digits > maxArray) stream.quit(Verdict::PE, expectation("Integer", input)); T result = 0; for(size_t i = 0; i < length; ++i){ result = result * radix + digits[i]; } if(negative){ result = -result; } return result; }
static int readChar(FILE *fp) { int c= getc(fp); if ('\\' == c) { c= getc(fp); switch (c) { case 'a': return '\a'; case 'b': return '\b'; case 'f': return '\f'; case 'n': return '\n'; case 'r': return '\r'; case 't': return '\t'; case 'v': return '\v'; case '\'': return '\''; case '"': return '"'; case '\\': return '\\'; case 'u': { int a= getc(fp), b= getc(fp), c= getc(fp), d= getc(fp); return (digitValue(a) << 24) + (digitValue(b) << 16) + (digitValue(c) << 8) + digitValue(d); } case 'x': { int x= 0; while (isHexadecimal(c= getc(fp))) x= x * 16 + digitValue(c); ungetc(c, fp); return x; } case '0' ... '7': { int x= 0; if (isOctal(c= getc(fp))) { x= x * 8 + digitValue(c); if (isOctal(c= getc(fp))) { x= x * 8 + digitValue(c); if (isOctal(c= getc(fp))) { x= x * 8 + digitValue(c); c= getc(fp); } } } ungetc(c, fp); return x; } default: fprintf(stderr, "illegal character escape: \\%c", c); exit(1); break; } }
int main() { char roman_Number[10]; int i = 0; int number = 0; scanf("%s", roman_Number); while(roman_Number[i]) { if(digitValue(roman_Number[i]) < 0) { printf("Error!"); return 0; } if((strlen(roman_Number) - i) > 2) { if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])) { printf("Error!"); return 0; } } if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1])) number = number + digitValue(roman_Number[i]); else { number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i])); i++; } i++; } if(number > 256) { printf("Number is higher than 256!"); return 404; } printf("%d", number); return 0; }
retCode g__digitCode(processPo P, ptrPo a) { ptrI x = deRefI(&a[1]); if (isvar(x)) return liberror(P, "__digitCode", eINSUFARG); else { codePoint ch = (codePoint)IntVal(x); if (isNdChar(ch)) { ptrI ans = allocateInteger(&P->proc.heap, digitValue(ch)); return equal(P, &a[2], &ans); } else return Fail; } }
int fancyAtoI(char *buffer, int base) { int value; int digit; char c; value = 0; while (*buffer != '\0') { if ((digit = digitValue(c = *buffer++)) >= base) { error(DIGIT_OUT_OF_RADIX_ERROR, c, base); return(0); } value = value*base + digit; } return(value); }
/* ** Convert an octal string to integer value ** This function stops converting either at the end of the input string ** or if it finds a non-octal digit. */ int octalValue(char const *octalString) { int numberValue = 0; int i=0, siz=0; char digit = 0; siz = strlen(octalString); for(i=0; i<siz; i++) { digit = octalString[i]; if (isOctalDigit(digit)) { numberValue <<= 3; // numberValue = numberValue * 8 numberValue += digitValue(digit); } else { break; } } return numberValue; }
double Lexer::parseInt(int base) { uint64_t bits = 0; uint32_t scale = 0; uint32_t k = 0; while (mark < idx && *mark == '0') mark++; if (mark==idx) return 0.0; switch (base) { case 8: for ( const wchar* i=mark ; i < idx ; i++ ) { if (k < 22) { bits = bits << 3 | digitValue(*i); k++; } scale += 3; } goto bits_and_scale; case 16: for ( const wchar* i=mark ; i < idx ; i++ ) { if (k < 16) { bits = bits << 4 | digitValue(*i); k++; } scale += 4; } goto bits_and_scale; case 10: return parseFloat(); default: compiler->internalError(lineno, "Unknown base in parseInt"); } bits_and_scale: // Left-adjust uint32_t n = scale; if (n < 33) { bits <<= 32; n += 32; } if (n < 49) { bits <<= 16; n += 16; } if (n < 57) { bits <<= 8; n += 8; } if (n < 61) { bits <<= 4; n += 4; } if (n < 63) { bits <<= 2; n += 2; } if (n < 64) { bits <<= 1; n += 1; } // Normalize if ((int64_t)bits > 0) { bits <<= 1; scale--; } if ((int64_t)bits > 0) { bits <<= 1; scale--; } if ((int64_t)bits > 0) { bits <<= 1; scale--; } // Get rid of implicit leading bit, shift into position bits <<= 1; uint64_t lost = bits & 0xFFF; bits >>= 12; scale--; // Round to even // FIME: it would seem necessary to re-normalize following rounding. if (lost > 0x800) bits += 1; else if (lost == 0x800) { if (bits & 1) bits += 1; } bits &= ~(uint64_t)0 >> 12; // Compute and insert exponent // FIXME: overflow integer constants properly to Infinity. bits |= (uint64_t)(1023 + scale) << 52; union { uint64_t bits; double d; } u; u.bits = bits; return u.d; }
void CrappyParser::parseChar(char c) { _hasNewKeyValue = false; if ( pstate == DONE_PARSING ) { return; } if ( pstate == WAIT_START_CONTENT ) { if ( waitForStart(c) ) { pstate = WAIT_START_OPEN; } } else if ( pstate == WAIT_START_OPEN ) { if ( c == '{' ) { pstate = WAIT_KEY_START; } } else if ( pstate == WAIT_KEY_START ) { if ( c == '\"' ) { pstate = WAIT_KEY; //key = ""; this->clearKeyBuff(); } } else if ( pstate == WAIT_KEY ) { if ( c == '\"' ) { pstate = WAIT_COLON; } else { //key += c; this->addCharToKey(c); //Serial.print(c); } } else if ( pstate == WAIT_COLON ) { if ( c == ':' ) { pstate = WAIT_VALUE_START; } } else if ( pstate == WAIT_VALUE_START ) { if ( c == '\"' ) { pstate = WAIT_VALUE; //value = ""; this->clearValBuff(); valueIsInteger = false; } else if ( isNumeric(c) ) { pstate = WAIT_VALUE; valueIsInteger = true; //value = ""; this->clearValBuff(); //value += c; intValue = 0; intValue = digitValue(c); //Serial.println(c); //Serial.println(intValue); } } else if ( pstate == WAIT_VALUE ) { if ( c == '\"' ) { pstate = WAIT_COMMA_OR_END; parsedKeyValue(); } else if ( valueIsInteger ) { if ( c == ' ') { pstate = WAIT_COMMA_OR_END; parsedKeyValue(); } else if (c == '}') { pstate = DONE_PARSING; parsedKeyValue(); doneParsing(); } else if (c == ',') { parsedKeyValue(); pstate = WAIT_KEY_START; } else if ( isNumeric(c) ) { intValue *= 10; intValue += digitValue(c); //Serial.println(c); //Serial.println(intValue); } } else { //value += c; this->addCharToVal(c); } } else if ( pstate == WAIT_COMMA_OR_END ) { if ( c == ',' ) { pstate = WAIT_KEY_START; } else if ( c == '}' ) { pstate = DONE_PARSING; doneParsing(); } } }
/** * Reads an integer number starting at the cursor in the expression string. * Number is read as a 64bit integer. */ bool MacroScanner::readNumber(int64_t *ret) { uint64_t retval = 0; int32_t base = 0; skipWhitespace(); if (!(isdigit(_cursor[0]))) { return false; } if ('0' == _cursor[0]) { _cursor += 1; if (('b' == _cursor[0]) || ('B' == _cursor[0])) { base = 2; _cursor += 1; } else if (('x' == _cursor[0]) || ('X' == _cursor[0])) { base = 16; _cursor += 1; } else { base = 8; } } else { base = 10; } while (isDigit(_cursor[0], base)) { retval *= base; retval += digitValue(_cursor[0]); _cursor += 1; } /* type suffixes do nothing since we treat each literal as an uint64_t. * so we just verify the suffix is correct */ bool seenL = false; bool seenU = false; for (int32_t i = 0; _cursor[0] && (i < 3); ++i, ++_cursor) { if (('l' == _cursor[0]) || ('L' == _cursor[0])) { if (seenL) { return false; } seenL = true; // case of 'L's must match if (_cursor[0] == _cursor[1]) { _cursor += 1; } else if (('l' == _cursor[1]) || ('L' == _cursor[1])) { return false; } } else if (('u' == _cursor[0]) || ('U' == _cursor[0])){ if (seenU) { return false; } seenU = true; } else { break; } } *ret = retval; return true; }