utility: fix for pointer/value error

Closes #459
This commit is contained in:
John McNamara 2024-10-24 19:53:40 +01:00
parent d9633436dc
commit 6ccd70c372
4 changed files with 84 additions and 39 deletions

View File

@ -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;
}
@ -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;
}
if (col_num)
col_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)
{
const char *p = row_str;
/* Find the : separator in the range. */
while (p && *p != ':')
p++;
if (p)
return lxw_name_to_row(++p);
else
return -1;
return col_num;
}
/*
@ -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;
}
/*

View File

@ -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;

View File

@ -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"));
@ -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"));

View File

@ -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"));
@ -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"));
}