Skip to content

Commit 705c02b

Browse files
Bug-fix with UTF-8 not correctly calculating the width
1 parent 42498c8 commit 705c02b

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- [ ] Updated delimiter to string instead of single char
2+
- [ ] Support UTF-8 delimiter
23
- [ ] Add support for more encodings
34
- [x] Implement all options for the printing
45
- [x] Add support for printing to file

custom_string.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ string_t *create_string(char *chars, int buffer_size) {
88
strncpy(string->chars, chars, buffer_size);
99
string->length = (int) strlen(chars);
1010
string->capacity = buffer_size;
11+
string->char_count = 0;
1112
return string;
1213
}
1314

@@ -16,21 +17,26 @@ string_t *create_string_empty(int buffer_size) {
1617
string->chars = (char *) malloc(buffer_size * sizeof(char));
1718
string->length = 0;
1819
string->capacity = buffer_size;
20+
string->char_count = 0;
1921
return string;
2022
}
2123

2224

23-
void append_char_to_string(string_t *string, char c) {
24-
if (string->length + 2 > string->capacity) {
25+
void append_char_to_string(string_t *string, char *c, int length) {
26+
while (string->length + 1 + length > string->capacity) {
2527
string->capacity *= 2;
2628
string->chars = (char *) realloc(string->chars, sizeof(*string->chars) * string->capacity);
2729
}
28-
string->chars[string->length] = c;
29-
string->length++;
30+
for (int i = 0; i < length; ++i) {
31+
string->chars[string->length] = (char) *c;
32+
string->length++;
33+
c++;
34+
}
35+
string->char_count++;
3036
string->chars[string->length] = '\0';
3137
}
3238

3339
void free_string(string_t *string) {
3440
free(string->chars);
3541
free(string);
36-
}
42+
}

custom_string.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define TABLE_PRINTER_CUSTOM_STRING_H
33
typedef struct {
44
char *chars;
5+
int char_count;
56
int length;
67
// -1 meaning not dynamic
78
int capacity;
@@ -11,7 +12,7 @@ string_t *create_string(char *chars, int buffer_size);
1112

1213
string_t *create_string_empty(int buffer_size);
1314

14-
void append_char_to_string(string_t *string, char c);
15+
void append_char_to_string(string_t *string, char *c, int length);
1516

1617
void free_string(string_t *string);
1718

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "string_table.h"
33
#include "options.h"
44

5-
#define VERSION_NAME "0.0.3"
5+
#define VERSION_NAME "0.0.4"
66

77
void print_version() {
88
printf("Table printer version: %s\n", VERSION_NAME);

string_table.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <stdio.h>
44
#include <stdint.h>
5+
#include <string.h>
56

67
string_table_t *create_string_table(int width) {
78
string_table_t *string_table = (string_table_t *) malloc(sizeof(string_table_t));
@@ -18,45 +19,70 @@ string_table_t *create_string_table(int width) {
1819

1920
string_table_t *get_string_table(options_t *options) {
2021
string_table_t *table = NULL;
21-
string_t *buffer = create_string_empty(12);
22-
string_array_t *line = create_string_array(3);
22+
string_t *field_buffer = create_string_empty(12);
23+
string_array_t *line_buffer = create_string_array(3);
24+
25+
string_t *char_buffer = NULL;
2326

2427
int input;
2528
int width = 0;
2629
while ((input = getc(options->stream)) != EOF) {
2730
if (input == options->delimiter) {
28-
add_string_to_array(line, buffer);
29-
buffer = create_string_empty(12);
31+
add_string_to_array(line_buffer, field_buffer);
32+
field_buffer = create_string_empty(12);
3033
width++;
3134
continue;
3235
}
3336
if (input == '\n' || input == '\r') {
34-
if(buffer->length == 0) continue;
35-
add_string_to_array(line, buffer);
36-
buffer = create_string_empty(12);
37+
if (field_buffer->length == 0) continue;
38+
add_string_to_array(line_buffer, field_buffer);
39+
field_buffer = create_string_empty(12);
3740
width++;
3841

3942
if (table == NULL) {
4043
table = create_string_table(width);
4144
}
42-
add_row_to_table(table, line);
43-
line = create_string_array(3);
45+
add_row_to_table(table, line_buffer);
46+
line_buffer = create_string_array(3);
4447
width = 0;
4548
} else {
46-
append_char_to_string(buffer, (char) input);
49+
50+
char current_char = (char) input;
51+
52+
if (current_char < 0) {
53+
unsigned char value = (unsigned char) current_char;
54+
if (value & 0x40) {
55+
if (char_buffer && char_buffer->length > 0) {
56+
append_char_to_string(field_buffer, char_buffer->chars, char_buffer->length);
57+
free_string(char_buffer);
58+
char_buffer = NULL;
59+
}
60+
}
61+
if (char_buffer == NULL) {
62+
char_buffer = create_string_empty(2);
63+
}
64+
append_char_to_string(char_buffer, &current_char, 1);
65+
continue;
66+
}
67+
if (char_buffer && char_buffer->length > 0) {
68+
append_char_to_string(field_buffer, char_buffer->chars, char_buffer->length);
69+
free_string(char_buffer);
70+
char_buffer = NULL;
71+
}
72+
append_char_to_string(field_buffer, &current_char, 1);
4773
}
4874
}
49-
free_string(buffer);
50-
free_string_array(line);
75+
free_string(field_buffer);
76+
free_string_array(line_buffer);
5177
return table;
5278
}
5379

5480
void recalculate_max_column_length(string_table_t *string_table, string_array_t *new_row) {
5581
for (int i = 0; i < string_table->width; ++i) {
5682
int max_length = string_table->max_column_length[i];
5783
if (i < new_row->length) {
58-
if (new_row->strings[i]->length > max_length) {
59-
max_length = new_row->strings[i]->length;
84+
if (new_row->strings[i]->char_count > max_length) {
85+
max_length = new_row->strings[i]->char_count;
6086
}
6187
} else {
6288
fprintf(stderr, "Error: column %d is not defined\n", i);
@@ -69,7 +95,7 @@ void add_row_to_table(string_table_t *string_table, string_array_t *new_row) {
6995
if (string_table->length + 1 > string_table->capacity) {
7096
string_table->capacity *= 2;
7197
string_table->rows = (string_array_t **) realloc(string_table->rows,
72-
sizeof(*string_table->rows) * string_table->capacity);
98+
sizeof(string_array_t *) * string_table->capacity);
7399
}
74100
recalculate_max_column_length(string_table, new_row);
75101
string_table->rows[string_table->length] = new_row;
@@ -154,7 +180,7 @@ void print_table(string_table_t *string_table, options_t *options) {
154180
}
155181
fprintf(options->out_stream, " ");
156182
string_t *string = row->strings[j];
157-
int padding = string_table->max_column_length[j] - string->length;
183+
int padding = string_table->max_column_length[j] - string->char_count;
158184
rotate_color(&color_index, COLOR_COUNT, options);
159185
print_aligned_string("%s", string->chars, padding, options->flags & ALIGN_LEFT, options);
160186
set_frame_color(options);

0 commit comments

Comments
 (0)