Commit c907e6c5 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Commonize mg_str

PUBLISHED_FROM=0e6a1c1a2356c1e12580e498c64ba7be367f601c
parent 29b3950e
...@@ -25,7 +25,6 @@ items: ...@@ -25,7 +25,6 @@ items:
- { type: file, name: mg_set_timer.md } - { type: file, name: mg_set_timer.md }
- { type: file, name: mg_time.md } - { type: file, name: mg_time.md }
- { type: file, name: mg_event_handler_t.md } - { type: file, name: mg_event_handler_t.md }
- { type: file, name: struct_mg_str.md }
- { type: file, name: struct_mg_mgr.md } - { type: file, name: struct_mg_mgr.md }
- { type: file, name: struct_mg_connection.md } - { type: file, name: struct_mg_connection.md }
- { type: file, name: struct_mg_add_sock_opts.md } - { type: file, name: struct_mg_add_sock_opts.md }
......
---
title: "struct mg_str"
decl_name: "struct mg_str"
symbol_kind: "struct"
signature: |
struct mg_str {
const char *p; /* Memory chunk pointer */
size_t len; /* Memory chunk length */
};
---
Describes chunk of memory
---
title: "MG_MK_STR()"
decl_name: "MG_MK_STR"
symbol_kind: "func"
signature: |
#define MG_MK_STR(str_literal);
---
Macro for initializing mg_str.
...@@ -6,8 +6,6 @@ items: ...@@ -6,8 +6,6 @@ items:
- { type: file, name: mg_skip.md } - { type: file, name: mg_skip.md }
- { type: file, name: mg_ncasecmp.md } - { type: file, name: mg_ncasecmp.md }
- { type: file, name: mg_casecmp.md } - { type: file, name: mg_casecmp.md }
- { type: file, name: mg_vcmp.md }
- { type: file, name: mg_vcasecmp.md }
- { type: file, name: mg_base64_decode.md } - { type: file, name: mg_base64_decode.md }
- { type: file, name: mg_base64_encode.md } - { type: file, name: mg_base64_encode.md }
- { type: file, name: mg_stat.md } - { type: file, name: mg_stat.md }
...@@ -23,8 +21,6 @@ items: ...@@ -23,8 +21,6 @@ items:
- { type: file, name: mg_is_big_endian.md } - { type: file, name: mg_is_big_endian.md }
- { type: file, name: mg_next_comma_list_entry.md } - { type: file, name: mg_next_comma_list_entry.md }
- { type: file, name: mg_match_prefix.md } - { type: file, name: mg_match_prefix.md }
- { type: file, name: mg_mk_str.md }
- { type: file, name: MG_MK_STR.md }
--- ---
......
---
title: "mg_mk_str()"
decl_name: "mg_mk_str"
symbol_kind: "func"
signature: |
struct mg_str mg_mk_str(const char *s);
---
A helper function for creating mg_str struct from plain C string.
`NULL` is allowed and becomes `{NULL, 0}`.
---
title: "mg_vcasecmp()"
decl_name: "mg_vcasecmp"
symbol_kind: "func"
signature: |
int mg_vcasecmp(const struct mg_str *str2, const char *str1);
---
Cross-platform version of `strncasecmp()` where first string is
specified by `struct mg_str`.
---
title: "mg_vcmp()"
decl_name: "mg_vcmp"
symbol_kind: "func"
signature: |
int mg_vcmp(const struct mg_str *str2, const char *str1);
---
Cross-platform version of `strcmp()` where where first string is
specified by `struct mg_str`.
...@@ -954,6 +954,41 @@ void mbuf_remove(struct mbuf *mb, size_t n) { ...@@ -954,6 +954,41 @@ void mbuf_remove(struct mbuf *mb, size_t n) {
#endif /* EXCLUDE_COMMON */ #endif /* EXCLUDE_COMMON */
#ifdef MG_MODULE_LINES #ifdef MG_MODULE_LINES
#line 1 "./src/../../common/mg_str.c"
#endif
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
/* Amalgamated: #include "common/mg_str.h" */
#include <string.h>
struct mg_str mg_mk_str(const char *s) {
struct mg_str ret = {s, 0};
if (s != NULL) ret.len = strlen(s);
return ret;
}
int mg_vcmp(const struct mg_str *str1, const char *str2) {
size_t n2 = strlen(str2), n1 = str1->len;
int r = memcmp(str1->p, str2, (n1 < n2) ? n1 : n2);
if (r == 0) {
return n1 - n2;
}
return r;
}
int mg_vcasecmp(const struct mg_str *str1, const char *str2) {
size_t n2 = strlen(str2), n1 = str1->len;
int r = mg_ncasecmp(str1->p, str2, (n1 < n2) ? n1 : n2);
if (r == 0) {
return n1 - n2;
}
return r;
}
#ifdef MG_MODULE_LINES
#line 1 "./src/../../common/sha1.c" #line 1 "./src/../../common/sha1.c"
#endif #endif
/* Copyright(c) By Steve Reid <steve@edmweb.com> */ /* Copyright(c) By Steve Reid <steve@edmweb.com> */
...@@ -7302,24 +7337,6 @@ int mg_casecmp(const char *s1, const char *s2) { ...@@ -7302,24 +7337,6 @@ int mg_casecmp(const char *s1, const char *s2) {
return mg_ncasecmp(s1, s2, (size_t) ~0); return mg_ncasecmp(s1, s2, (size_t) ~0);
} }
int mg_vcasecmp(const struct mg_str *str1, const char *str2) {
size_t n2 = strlen(str2), n1 = str1->len;
int r = mg_ncasecmp(str1->p, str2, (n1 < n2) ? n1 : n2);
if (r == 0) {
return n1 - n2;
}
return r;
}
int mg_vcmp(const struct mg_str *str1, const char *str2) {
size_t n2 = strlen(str2), n1 = str1->len;
int r = memcmp(str1->p, str2, (n1 < n2) ? n1 : n2);
if (r == 0) {
return n1 - n2;
}
return r;
}
#ifndef MG_DISABLE_FILESYSTEM #ifndef MG_DISABLE_FILESYSTEM
int mg_stat(const char *path, cs_stat_t *st) { int mg_stat(const char *path, cs_stat_t *st) {
#ifdef _WIN32 #ifdef _WIN32
...@@ -7641,12 +7658,6 @@ int mg_match_prefix(const char *pattern, int pattern_len, const char *str) { ...@@ -7641,12 +7658,6 @@ int mg_match_prefix(const char *pattern, int pattern_len, const char *str) {
const struct mg_str pstr = {pattern, (size_t) pattern_len}; const struct mg_str pstr = {pattern, (size_t) pattern_len};
return mg_match_prefix_n(pstr, mg_mk_str(str)); return mg_match_prefix_n(pstr, mg_mk_str(str));
} }
struct mg_str mg_mk_str(const char *s) {
struct mg_str ret = {s, 0};
if (s != NULL) ret.len = strlen(s);
return ret;
}
#ifdef MG_MODULE_LINES #ifdef MG_MODULE_LINES
#line 1 "./src/mqtt.c" #line 1 "./src/mqtt.c"
#endif #endif
......
...@@ -841,6 +841,53 @@ double cs_time(); ...@@ -841,6 +841,53 @@ double cs_time();
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* CS_COMMON_CS_TIME_H_ */ #endif /* CS_COMMON_CS_TIME_H_ */
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_COMMON_MG_STR_H_
#define CS_COMMON_MG_STR_H_
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Describes chunk of memory */
struct mg_str {
const char *p; /* Memory chunk pointer */
size_t len; /* Memory chunk length */
};
/*
* A helper function for creating mg_str struct from plain C string.
* `NULL` is allowed and becomes `{NULL, 0}`.
*/
struct mg_str mg_mk_str(const char *s);
/* Macro for initializing mg_str. */
#define MG_MK_STR(str_literal) \
{ str_literal, sizeof(str_literal) - 1 }
/*
* Cross-platform version of `strcmp()` where where first string is
* specified by `struct mg_str`.
*/
int mg_vcmp(const struct mg_str *str2, const char *str1);
/*
* Cross-platform version of `strncasecmp()` where first string is
* specified by `struct mg_str`.
*/
int mg_vcasecmp(const struct mg_str *str2, const char *str1);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_COMMON_MG_STR_H_ */
/* /*
* Copyright (c) 2015 Cesanta Software Limited * Copyright (c) 2015 Cesanta Software Limited
* All rights reserved * All rights reserved
...@@ -1140,12 +1187,6 @@ union socket_address { ...@@ -1140,12 +1187,6 @@ union socket_address {
#endif #endif
}; };
/* Describes chunk of memory */
struct mg_str {
const char *p; /* Memory chunk pointer */
size_t len; /* Memory chunk length */
};
struct mg_connection; struct mg_connection;
/* /*
...@@ -1793,18 +1834,6 @@ int mg_ncasecmp(const char *s1, const char *s2, size_t len); ...@@ -1793,18 +1834,6 @@ int mg_ncasecmp(const char *s1, const char *s2, size_t len);
*/ */
int mg_casecmp(const char *s1, const char *s2); int mg_casecmp(const char *s1, const char *s2);
/*
* Cross-platform version of `strcmp()` where where first string is
* specified by `struct mg_str`.
*/
int mg_vcmp(const struct mg_str *str2, const char *str1);
/*
* Cross-platform version of `strncasecmp()` where first string is
* specified by `struct mg_str`.
*/
int mg_vcasecmp(const struct mg_str *str2, const char *str1);
/* /*
* Decode base64-encoded string `s`, `len` into the destination `dst`. * Decode base64-encoded string `s`, `len` into the destination `dst`.
* Destination has to have enough space to hold decoded buffer. * Destination has to have enough space to hold decoded buffer.
...@@ -1963,16 +1992,6 @@ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val, ...@@ -1963,16 +1992,6 @@ const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
int mg_match_prefix(const char *pattern, int pattern_len, const char *str); int mg_match_prefix(const char *pattern, int pattern_len, const char *str);
int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str); int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str);
/*
* A helper function for creating mg_str struct from plain C string.
* `NULL` is allowed and becomes `{NULL, 0}`.
*/
struct mg_str mg_mk_str(const char *s);
/* Macro for initializing mg_str. */
#define MG_MK_STR(str_literal) \
{ str_literal, sizeof(str_literal) - 1 }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment