Skip to content

Instantly share code, notes, and snippets.

View robertdavidgraham's full-sized avatar

Robert David Graham robertdavidgraham

View GitHub Profile
@robertdavidgraham
robertdavidgraham / robert_3.c
Created March 11, 2013 18:49
This file is an update to the "robert_2.c" file located here: https://github.com/regehr/str2long_contest This is a contest for parsing integers with no integer overflows. The performance tests use mostly invalid input, which skews the performance results. My "robert_2.c" code is half as fast as the fastest "yang_2.c" code. That's because Yang in…
#include "str2long.h"
long str2long_robert_3(const char *str)
{
long result = 0;
int i;
if (str[0] == '-') {
if (str[1] == '\0')
error = 1;
@robertdavidgraham
robertdavidgraham / str2long.c
Created March 9, 2013 04:26
This file in response to the programming contest at: http://blog.regehr.org/archives/909 The idea is to parse "long int" integers in C, with the following conditions: - accept all valid input - reject all invalid input (such as non-digit characters before the nul termination) - never have an integer overflow in the code The last part is the tric…
/*
* Code for programming contest at:
* http://blog.regehr.org/archives/909
*/
#include <limits.h>
extern int error;
long str2long(const char *str)
{
long result = 0;