Skip to content

Commit

Permalink
utility: fix for pointer/value error
Browse files Browse the repository at this point in the history
Closes #459
  • Loading branch information
jmcnamara committed Oct 24, 2024
1 parent d963343 commit 6ccd70c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 39 deletions.
79 changes: 44 additions & 35 deletions src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,39 @@ lxw_row_t
lxw_name_to_row(const char *row_str)
{
lxw_row_t row_num = 0;
const char *p = row_str;

if (!row_str)
return row_num;

/* Skip the column letters and absolute symbol of the A1 cell. */
while (p && !isdigit((unsigned char) *p))
p++;
while (*row_str && !isdigit((unsigned char) *row_str))
row_str++;

/* Convert the row part of the A1 cell to a number. */
if (p)
row_num = atoi(p);
if (*row_str)
row_num = atoi(row_str);

if (row_num)
return row_num - 1;
row_num--;

return row_num;
}

/*
* Convert the second row of an Excel range ref to a zero indexed number.
*/
uint32_t
lxw_name_to_row_2(const char *row_str)
{
if (!row_str)
return 0;

/* Find the : separator in the range. */
while (*row_str && *row_str != ':')
row_str++;

if (*row_str)
return lxw_name_to_row(++row_str);
else
return 0;
}
Expand All @@ -275,34 +296,21 @@ lxw_col_t
lxw_name_to_col(const char *col_str)
{
lxw_col_t col_num = 0;
const char *p = col_str;

if (!col_str)
return col_num;

/* Convert leading column letters of A1 cell. Ignore absolute $ marker. */
while (p && (isupper((unsigned char) *p) || *p == '$')) {
if (*p != '$')
col_num = (col_num * 26) + (*p - 'A' + 1);
p++;
while (*col_str && (isupper((unsigned char) *col_str) || *col_str == '$')) {
if (*col_str != '$')
col_num = (col_num * 26) + (*col_str - 'A' + 1);
col_str++;
}

return col_num - 1;
}

/*
* Convert the second row of an Excel range ref to a zero indexed number.
*/
uint32_t
lxw_name_to_row_2(const char *row_str)
{
const char *p = row_str;

/* Find the : separator in the range. */
while (p && *p != ':')
p++;
if (col_num)
col_num--;

if (p)
return lxw_name_to_row(++p);
else
return -1;
return col_num;
}

/*
Expand All @@ -311,16 +319,17 @@ lxw_name_to_row_2(const char *row_str)
uint16_t
lxw_name_to_col_2(const char *col_str)
{
const char *p = col_str;
if (!col_str)
return 0;

/* Find the : separator in the range. */
while (p && *p != ':')
p++;
while (*col_str && *col_str != ':')
col_str++;

if (p)
return lxw_name_to_col(++p);
if (*col_str)
return lxw_name_to_col(++col_str);
else
return -1;
return 0;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/xmlwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ lxw_escape_data(const char *data)
uint8_t
lxw_has_control_characters(const char *string)
{
while (string) {
while (*string) {
/* 0xE0 == 0b11100000 masks values > 0x19 == 0b00011111. */
if (!(*string & 0xE0) && *string != 0x0A && *string != 0x09)
return LXW_TRUE;
Expand Down
12 changes: 10 additions & 2 deletions test/unit/utility/test_name_to_col.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

#include "../../../include/xlsxwriter/utility.h"

// Test _xl_get_col().
// Test lxw_name_to_col().
CTEST(utility, lxw_name_to_col) {

ASSERT_EQUAL(0, lxw_name_to_col(NULL));
ASSERT_EQUAL(0, lxw_name_to_col(""));
ASSERT_EQUAL(0, lxw_name_to_col("1"));
ASSERT_EQUAL(0, lxw_name_to_col("A"));
ASSERT_EQUAL(0, lxw_name_to_col("A1"));
ASSERT_EQUAL(1, lxw_name_to_col("B1"));
ASSERT_EQUAL(2, lxw_name_to_col("C1"));
Expand All @@ -32,9 +36,13 @@ CTEST(utility, lxw_name_to_col) {
}


// Test _xl_get_col_2().
// Test lxw_name_to_col_2().
CTEST(utility, lxw_name_to_col_2) {

ASSERT_EQUAL(0, lxw_name_to_col_2(NULL));
ASSERT_EQUAL(0, lxw_name_to_col_2(""));
ASSERT_EQUAL(0, lxw_name_to_col_2("AAA"));
ASSERT_EQUAL(0, lxw_name_to_col_2("AAA:"));
ASSERT_EQUAL(0, lxw_name_to_col_2("AAA:A"));
ASSERT_EQUAL(1, lxw_name_to_col_2("AAA:B"));
ASSERT_EQUAL(2, lxw_name_to_col_2("AAA:C"));
Expand Down
30 changes: 29 additions & 1 deletion test/unit/utility/test_xl_name_to_row.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

#include "../../../include/xlsxwriter/utility.h"

// Test _xl_get_row().
// Test lxw_name_to_row().
CTEST(utility, lxw_name_to_row) {

ASSERT_EQUAL(0, lxw_name_to_row(NULL));
ASSERT_EQUAL(0, lxw_name_to_row(""));
ASSERT_EQUAL(0, lxw_name_to_row("A"));
ASSERT_EQUAL(0, lxw_name_to_row("A0"));
ASSERT_EQUAL(0, lxw_name_to_row("A1"));
ASSERT_EQUAL(0, lxw_name_to_row("$A$1"));
ASSERT_EQUAL(1, lxw_name_to_row("B2"));
Expand All @@ -31,3 +35,27 @@ CTEST(utility, lxw_name_to_row) {
ASSERT_EQUAL(1048576, lxw_name_to_row("$XFE$1048577"));
}

// Test lxw_name_to_row().
CTEST(utility, lxw_name_to_row_2) {

ASSERT_EQUAL(0, lxw_name_to_row_2(NULL));
ASSERT_EQUAL(0, lxw_name_to_row_2(""));
ASSERT_EQUAL(0, lxw_name_to_row_2("A1:A"));
ASSERT_EQUAL(0, lxw_name_to_row_2("A1:A0"));
ASSERT_EQUAL(0, lxw_name_to_row_2("A1:A1"));
ASSERT_EQUAL(0, lxw_name_to_row_2("A1:$A$1"));
ASSERT_EQUAL(1, lxw_name_to_row_2("A1:B2"));
ASSERT_EQUAL(2, lxw_name_to_row_2("A1:C3"));
ASSERT_EQUAL(9, lxw_name_to_row_2("A1:J10"));
ASSERT_EQUAL(24, lxw_name_to_row_2("A1:Y25"));
ASSERT_EQUAL(25, lxw_name_to_row_2("A1:Z26"));
ASSERT_EQUAL(26, lxw_name_to_row_2("A1:AA27"));
ASSERT_EQUAL(254, lxw_name_to_row_2("A1:IU255"));
ASSERT_EQUAL(255, lxw_name_to_row_2("A1:IV256"));
ASSERT_EQUAL(256, lxw_name_to_row_2("A1:IW257"));
ASSERT_EQUAL(16383, lxw_name_to_row_2("A1:XFD16384"));
ASSERT_EQUAL(16384, lxw_name_to_row_2("A1:XFE16385"));
ASSERT_EQUAL(1048576, lxw_name_to_row_2("A1:XFE1048577"));
ASSERT_EQUAL(1048576, lxw_name_to_row_2("A1:$XFE$1048577"));
}

0 comments on commit 6ccd70c

Please sign in to comment.