62 lines
1.1 KiB
Markdown
62 lines
1.1 KiB
Markdown
# extract_fence_ext.c
|
|
|
|
Extract the extension from a markdown code fence.
|
|
|
|
---
|
|
|
|
## Includes
|
|
```c
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
#include "utils.h"
|
|
```
|
|
|
|
---
|
|
|
|
## Function Description
|
|
|
|
### `extract_fence_ext`
|
|
Extracts the extension from a markdown code fence string.
|
|
|
|
#### Parameter
|
|
- `fence`: Pointer to a null-terminated string representing the code fence.
|
|
|
|
#### Return Value
|
|
|
|
Returns a dynamically allocated string containing the extracted extension, or
|
|
`NULL` if no extension is found or if the input is not a valid code fence.
|
|
|
|
Return value must be freed by the caller.
|
|
|
|
#### Implementation
|
|
|
|
```c
|
|
char *
|
|
extract_fence_ext(const char *fence)
|
|
{
|
|
const char *start;
|
|
const char *end;
|
|
size_t len;
|
|
char *ext;
|
|
|
|
if (0 != strncmp(fence, "```", 3))
|
|
return (NULL);
|
|
start = fence + 3;
|
|
while (' ' == *start || '\t' == *start)
|
|
start++;
|
|
if ('\n' == *start || '\0' == *start)
|
|
return (NULL);
|
|
end = start;
|
|
while ('\n' != *end && '\0' != *end && ' ' != *end && '\t' != *end)
|
|
end++;
|
|
len = (size_t)(end - start);
|
|
ext = malloc(len + 1);
|
|
if (NULL == ext)
|
|
return (NULL);
|
|
memcpy(ext, start, len);
|
|
ext[len] = '\0';
|
|
return (ext);
|
|
}
|
|
```
|