Skip to content

Commit

Permalink
now returns true for negative numbers and false for negative zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Ferdman committed Jan 18, 2018
1 parent 4477bff commit 62a8748
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
32 changes: 16 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env python3
# encoding: utf-8
from distutils.core import setup, Extension

strextnd_module = Extension('strextnd', sources = ['strextnd.c'])

setup(name='strextnd',
version='0.1',
description='A Module written in C that extends the python3 str library',
author = 'Ariel Ferdman',
author_email = 'arielxcon@gmail.com',
ext_modules=[strextnd_module],
url = 'https://github.com/arielferdman/strextnd',
download_url = 'https://github.com/arielferdman/strextnd/archive/0.1.tar.gz',
keywords = ['string', 'is_numeric', 'float'],
classifiers = [],)
#!/usr/bin/env python3
# encoding: utf-8
from distutils.core import setup, Extension

strextnd_module = Extension('strextnd', sources = ['strextnd.c'])

setup(name='strextnd',
version='0.2',
description='A Module written in C that extends the python3 str library',
author = 'Ariel Ferdman',
author_email = 'arielxcon@gmail.com',
ext_modules=[strextnd_module],
url = 'https://github.com/arielferdman/strextnd',
download_url = 'https://github.com/arielferdman/strextnd/archive/0.1.tar.gz',
keywords = ['string', 'is_numeric', 'float'],
classifiers = [],)
29 changes: 23 additions & 6 deletions strextnd.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
#include <Python.h>

int _is_numeric(const char *str) {
int str_len = strlen(str);
int is_numeric(char *str) {
int dot_flag = 0;
int all_zeros = 1;
int leading_minus = 0;
int cursor;
int ascii_value;

if (*(str) == 0) {
if (((int)*(str) < 48 || (int)*(str) > 57) && *(str) != 45) {
return 0;
}

for (cursor = 0; cursor < str_len; cursor++) {
if ((int)*(str) == 45 && (int)*(str + 1) == 48 && *(str + 2) == 0) {
return 0;
}

if ((int)*(str) == 45) {
leading_minus = 1;
}

for (cursor = 1; *(str + cursor) != 0; cursor++) {
ascii_value = (int)*(str + cursor);
if (ascii_value == 46 && dot_flag == 1) {
return 0;
}
if ((ascii_value == 46 && cursor == 0) || (ascii_value == 46 && cursor == str_len - 1)) {
if (ascii_value == 46 && *(str + cursor + 1) == 0) {
return 0;
}
if (ascii_value == 46 && dot_flag == 0) {
Expand All @@ -25,6 +34,14 @@ int _is_numeric(const char *str) {
if (ascii_value < 48 || ascii_value > 57) {
return 0;
}

if (ascii_value != 48) {
all_zeros = 0;
}
}

if (all_zeros == 1 && leading_minus == 1) {
return 0;
}

return 1;
Expand Down Expand Up @@ -56,4 +73,4 @@ static struct PyModuleDef is_numeric_definition = {
PyMODINIT_FUNC PyInit_strextnd(void) {
Py_Initialize();
return PyModule_Create(&is_numeric_definition);
}
}

0 comments on commit 62a8748

Please sign in to comment.