| 1 | #ifndef KDL_COMMON_H_ | ||
| 2 | #define KDL_COMMON_H_ | ||
| 3 | |||
| 4 | #include "common-macros.h" | ||
| 5 | #include <stddef.h> | ||
| 6 | |||
| 7 | #ifdef __cplusplus | ||
| 8 | extern "C" { | ||
| 9 | #endif // __cplusplus | ||
| 10 | |||
| 11 | // Parameter for kdl_escape - which characters should be escaped, and which should not? | ||
| 12 | enum kdl_escape_mode { | ||
| 13 | KDL_ESCAPE_MINIMAL = 0, // Only escape what *must* be escaped | ||
| 14 | KDL_ESCAPE_CONTROL = 0x10, // Escape ASCII control characters | ||
| 15 | KDL_ESCAPE_NEWLINE = 0x20, // Escape newline characters | ||
| 16 | KDL_ESCAPE_TAB = 0x40, // Escape tabs | ||
| 17 | KDL_ESCAPE_ASCII_MODE = 0x170, // Escape all non-ASCII charscters | ||
| 18 | // "Sensible" default: escape tabs, newlines, and other control characters, but leave | ||
| 19 | // unicode intact | ||
| 20 | KDL_ESCAPE_DEFAULT = KDL_ESCAPE_CONTROL | KDL_ESCAPE_NEWLINE | KDL_ESCAPE_TAB | ||
| 21 | }; | ||
| 22 | |||
| 23 | enum kdl_version { | ||
| 24 | KDL_VERSION_1, | ||
| 25 | KDL_VERSION_2 | ||
| 26 | }; | ||
| 27 | |||
| 28 | // Function pointers used to interface with external IO | ||
| 29 | typedef size_t (*kdl_read_func)(void* user_data, char* buf, size_t bufsize); | ||
| 30 | typedef size_t (*kdl_write_func)(void* user_data, char const* data, size_t nbytes); | ||
| 31 | |||
| 32 | typedef struct kdl_str kdl_str; | ||
| 33 | typedef struct kdl_owned_string kdl_owned_string; | ||
| 34 | typedef enum kdl_escape_mode kdl_escape_mode; | ||
| 35 | typedef enum kdl_version kdl_version; | ||
| 36 | |||
| 37 | // A reference to a string, like Rust's str or C++'s std::u8string_view | ||
| 38 | // Need not be nul-terminated! | ||
| 39 | struct kdl_str { | ||
| 40 | char const* data; // Data pointer - NULL means no string | ||
| 41 | size_t len; // Length of the string - 0 means empty string | ||
| 42 | }; | ||
| 43 | |||
| 44 | // An owned string. Should be destroyed using kdl_free_string | ||
| 45 | // Owned strings are nul-terminated. | ||
| 46 | struct kdl_owned_string { | ||
| 47 | char* data; | ||
| 48 | size_t len; | ||
| 49 | }; | ||
| 50 | |||
| 51 | // Get a reference to an owned string | ||
| 52 | 15737 | KDL_EXPORT_INLINE kdl_str kdl_borrow_str(kdl_owned_string const* str) | |
| 53 | { | ||
| 54 | 15737 | kdl_str result = {str->data, str->len}; | |
| 55 | 15737 | return result; | |
| 56 | } | ||
| 57 | |||
| 58 | // Create a kdl_str from a nul-terminated C string | ||
| 59 | KDL_EXPORT kdl_str kdl_str_from_cstr(char const* s); | ||
| 60 | // Create an owned string with the same content as another string | ||
| 61 | KDL_NODISCARD KDL_EXPORT kdl_owned_string kdl_clone_str(kdl_str const* s); | ||
| 62 | // Free the memory associated with an owned string, and set the pointer to NULL | ||
| 63 | KDL_EXPORT void kdl_free_string(kdl_owned_string* s); | ||
| 64 | |||
| 65 | // Escape special characters in a string | ||
| 66 | KDL_NODISCARD KDL_EXPORT kdl_owned_string kdl_escape_v(kdl_version version, kdl_str const* s, kdl_escape_mode mode); | ||
| 67 | // Resolve backslash escape sequences | ||
| 68 | KDL_NODISCARD KDL_EXPORT kdl_owned_string kdl_unescape_v(kdl_version version, kdl_str const* s); | ||
| 69 | // Resolve backslash escape sequences and (in v2) dedent string | ||
| 70 | KDL_NODISCARD KDL_EXPORT kdl_owned_string kdl_unescape_multi_line(kdl_version version, kdl_str const* s); | ||
| 71 | |||
| 72 | // Escape special characters in a string according to KDLv1 string rules | ||
| 73 | KDL_DEPRECATED("Use kdl_escape_v instead") | ||
| 74 | KDL_NODISCARD KDL_EXPORT kdl_owned_string kdl_escape(kdl_str const* s, kdl_escape_mode mode); | ||
| 75 | // Resolve backslash escape sequences according to KDLv1 rules | ||
| 76 | KDL_DEPRECATED("Use kdl_unescape_v instead") | ||
| 77 | KDL_NODISCARD KDL_EXPORT kdl_owned_string kdl_unescape(kdl_str const* s); | ||
| 78 | |||
| 79 | #ifdef __cplusplus | ||
| 80 | } | ||
| 81 | #endif // __cplusplus | ||
| 82 | |||
| 83 | #endif // KDL_COMMON_H_ |