Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
suconghou committed Jun 2, 2023
1 parent be0e10f commit bb9f2c0
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 143 deletions.
126 changes: 65 additions & 61 deletions c++/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,95 +124,92 @@ class Line
int
parse_item_trim_space(char *item_value, char_is_match cond, int strip_square, int result_strip_left_quote)
{
int pad = 1;
while (index < len)
{
const unsigned char x = str[index];
if (x == ' ' || (strip_square && x == '['))
{
index++;
}
else
{
break;
}
}
int found_start = -1;
int found_end = -1;
int gotvalue = -1;
int i = index;
while (i < len)
{
const unsigned char x = str[i];
i++;
if ((x == ' ' || (strip_square && x == '[')) && pad)
const unsigned char y = i < len ? str[i] : 0;
const unsigned char z = i >= 2 ? str[i - 2] : 0;
if (cond(x, y, z))
{
index = i;
continue;
found_end = i - 1;
if (found_start < 0)
{
found_start = found_end;
}
if (i < len)
{
continue;
}
}
else
if (found_start < 0)
{
pad = 0;
return -1;
}
if (gotvalue < 0)
// 包含cond成立时当前字符x,如果结果字符串以某字符开始,我们配置了result_strip_left_quote开头
// 例如解析request_line,开头有引号,我们仅在生成结果时过滤
if (result_strip_left_quote > 0)
{
const unsigned char y = i < len ? str[i] : 0;
const unsigned char z = i >= 2 ? str[i - 2] : 0;
if (cond(x, y, z))
while (found_start < found_end)
{
found_end = i - 1;
if (found_start < 0)
if (str[found_start] == 34)
{
found_start = found_end;
found_start++;
}
if (i < len)
else
{
continue;
break;
}
}
if (found_start < 0)
{
goto end;
}
// 包含cond成立时当前字符x,如果结果字符串以某字符开始,我们配置了result_strip_left_quote开头
// 例如解析request_line,开头有引号,我们仅在生成结果时过滤
if (result_strip_left_quote > 0)
}
const int v_len = found_end - found_start + 1;
strncpy(item_value, str + found_start, v_len);
item_value[v_len] = '\0';
if (i >= len)
{
if (found_end == len - 1 || x == ' ')
{
while (found_start < found_end)
{
if (str[found_start] == 34)
{
found_start++;
}
else
{
break;
}
}
// 字符串已完全遍历
index = len;
}
const int v_len = found_end - found_start + 1;
strncpy(item_value, str + found_start, v_len);
item_value[v_len] = '\0';
gotvalue = 1;
if (i >= len)
else
{
if (found_end == len - 1 || x == ' ')
{
// 字符串已完全遍历
index = len;
}
else
{
index = found_end + 1;
}
index = found_end + 1;
}
}
else
{
if (x == ' ' || (strip_square && x == ']'))
while (i < len)
{
if (i < len)
const unsigned char x = str[i];
if (x == ' ' || (strip_square && x == ']'))
{
continue;
i++;
}
else if (i == len)
else
{
i++;
break;
}
}
index = i - 1;
break;
index = i;
}
return found_start;
}
end:
return found_start;
}

Expand All @@ -226,19 +223,17 @@ class Line

int parse_remote_user(char *item_value)
{
int i = index;
while (i < len)
while (index < len)
{
if (str[i] == '-')
if (str[index] == '-')
{
i++;
index++;
}
else
{
break;
}
}
index = i;
return parse_item_trim_space(item_value, not_space, 0, 0);
}

Expand Down Expand Up @@ -407,14 +402,23 @@ int process(istream &fh)
unsigned int total_lines = 0;

strMap remote_addr_data;
remote_addr_data.reserve(8192);
strMap remote_user_data;
remote_user_data.reserve(64);
strMap time_local_data;
time_local_data.reserve(16384);
strMap request_line_data;
request_line_data.reserve(16384);
strMap status_data;
status_data.reserve(64);
strMap http_referer_data;
http_referer_data.reserve(8192);
strMap http_user_agent_data;
http_user_agent_data.reserve(8192);
strMap http_x_forwarded_for_data;
http_x_forwarded_for_data.reserve(2048);
strMap http_sent_data;
http_sent_data.reserve(16384);
statusMap http_bad_code_data;

while (getline(fh, str))
Expand Down
110 changes: 54 additions & 56 deletions c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,95 +106,93 @@ int digital_or_none_end(unsigned char x, unsigned char y, unsigned char z)
// offset 字符串坐标
int parse_item_trim_space(const char *s, int *offset, int len, char *item_value, char_is_match cond, int strip_square, int result_strip_left_quote)
{
int pad = 1;
int found_start = -1;
int found_end = -1;
int gotvalue = -1;
int i = *offset;
while (i < len)
{
unsigned char x = s[i];
i++;
if ((x == ' ' || (strip_square && x == '[')) && pad)
if (x == ' ' || (strip_square && x == '['))
{
*offset = i;
continue;
i++;
}
else
{
pad = 0;
break;
}
if (gotvalue < 0)
}
*offset = i;
int found_start = -1;
int found_end = -1;
while (i < len)
{
const unsigned char x = s[i];
i++;
const unsigned char y = i < len ? s[i] : 0;
const unsigned char z = i >= 2 ? s[i - 2] : 0;
if (cond(x, y, z))
{
const unsigned char y = i < len ? s[i] : 0;
const unsigned char z = i >= 2 ? s[i - 2] : 0;
if (cond(x, y, z))
{
found_end = i - 1;
if (found_start < 0)
{
found_start = found_end;
}
if (i < len)
{
continue;
}
}
found_end = i - 1;
if (found_start < 0)
{
goto end;
found_start = found_end;
}
// 包含cond成立时当前字符x,如果结果字符串以某字符开始,我们配置了result_strip_left_quote开头
// 例如解析request_line,开头有引号,我们仅在生成结果时过滤
if (result_strip_left_quote > 0)
if (i < len)
{
while (found_start < found_end)
{
if (s[found_start] == 34)
{
found_start++;
}
else
{
break;
}
}
continue;
}
const int v_len = found_end - found_start + 1;
strncpy(item_value, s + found_start, v_len);
item_value[v_len] = '\0';
gotvalue = 1;
if (i >= len)
}
if (found_start < 0)
{
return -1;
}
// 包含cond成立时当前字符x,如果结果字符串以某字符开始,我们配置了result_strip_left_quote开头
// 例如解析request_line,开头有引号,我们仅在生成结果时过滤
if (result_strip_left_quote > 0)
{
while (found_start < found_end)
{
if (found_end == len - 1 || x == ' ')
if (s[found_start] == 34)
{
// 字符串已完全遍历
*offset = len;
found_start++;
}
else
{
*offset = found_end + 1;
break;
}
}
}
const int v_len = found_end - found_start + 1;
strncpy(item_value, s + found_start, v_len);
item_value[v_len] = '\0';
if (i >= len)
{
if (found_end == len - 1 || x == ' ')
{
// 字符串已完全遍历
*offset = len;
}
else
{
*offset = found_end + 1;
}
}
else
{
if (x == ' ' || (strip_square && x == ']'))
while (i < len)
{
if (i < len)
const unsigned char x = s[i];
if (x == ' ' || (strip_square && x == ']'))
{
continue;
i++;
}
else if (i == len)
else
{
i++;
break;
}
}
*offset = i - 1;
break;
*offset = i;
}
return found_start;
}
end:
return found_start;
}

Expand Down
Loading

0 comments on commit bb9f2c0

Please sign in to comment.