src/codegens.c

Thu, 25 Aug 2016 12:16:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 25 Aug 2016 12:16:57 +0200
changeset 51
f25ba6fd7a08
parent 49
f86f0b054464
permissions
-rw-r--r--

replaces stack buffers with UCX buffers

universe@21 1 /*
universe@21 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@21 3 *
universe@35 4 * Copyright 2016 Mike Becker. All rights reserved.
universe@21 5 *
universe@21 6 * Redistribution and use in source and binary forms, with or without
universe@21 7 * modification, are permitted provided that the following conditions are met:
universe@21 8 *
universe@21 9 * 1. Redistributions of source code must retain the above copyright
universe@21 10 * notice, this list of conditions and the following disclaimer.
universe@21 11 *
universe@21 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@21 13 * notice, this list of conditions and the following disclaimer in the
universe@21 14 * documentation and/or other materials provided with the distribution.
universe@21 15 *
universe@21 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@21 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@21 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@21 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@21 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@21 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@21 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@21 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@21 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@21 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@21 26 * POSSIBILITY OF SUCH DAMAGE.
universe@21 27 *
universe@21 28 */
universe@21 29
universe@21 30 #include "codegens.h"
universe@21 31
universe@51 32 HighlighterData* new_highlighter_data() {
universe@51 33 HighlighterData* hd = malloc(sizeof(HighlighterData));
universe@51 34 if (hd == NULL) {
universe@51 35 return NULL;
universe@51 36 } else {
universe@51 37 hd->multiline_comment = 0;
universe@51 38 hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
universe@51 39 hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
universe@51 40 return hd;
universe@51 41 }
universe@51 42 }
universe@51 43
universe@51 44 void free_highlighter_data(HighlighterData *hd) {
universe@51 45 ucx_buffer_free(hd->primary_buffer);
universe@51 46 ucx_buffer_free(hd->secondary_buffer);
universe@51 47 free(hd);
universe@51 48 }
universe@51 49
universe@48 50 void put_htmlescaped(UcxBuffer *dest, char c) {
universe@21 51 if (c == '>') {
universe@48 52 ucx_buffer_puts(dest, "&gt;");
universe@21 53 } else if (c == '<') {
universe@48 54 ucx_buffer_puts(dest, "&lt;");
universe@21 55 } else {
universe@48 56 ucx_buffer_putc(dest, c);
universe@21 57 }
universe@48 58 }
universe@21 59
universe@48 60 void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
universe@48 61 for (int i = 0 ; i < s.length ; i++) {
universe@48 62 put_htmlescaped(dest, s.ptr[i]);
universe@48 63 }
universe@21 64 }
universe@21 65
universe@47 66 int check_keyword(sstr_t word, const char** keywords) {
universe@21 67 for (int i = 0 ; keywords[i] ; i++) {
universe@47 68 if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
universe@21 69 return 1;
universe@21 70 }
universe@21 71 }
universe@21 72 return 0;
universe@21 73 }
universe@21 74
universe@47 75 int check_capsonly(sstr_t word) {
universe@47 76 for (size_t i = 0 ; i < word.length ; i++) {
universe@47 77 if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
universe@47 78 && word.ptr[i] != '_') {
universe@21 79 return 0;
universe@21 80 }
universe@21 81 }
universe@21 82 return 1;
universe@21 83 }

mercurial