diff options
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/cmdline.cpp | 1 | ||||
-rw-r--r-- | src/error.h | 1 | ||||
-rw-r--r-- | src/goto.cpp | 1 | ||||
-rw-r--r-- | src/main.cpp | 23 | ||||
-rw-r--r-- | src/parser.cpp | 1 | ||||
-rw-r--r-- | src/qregisters.cpp | 1 | ||||
-rw-r--r-- | src/sciteco.h | 49 | ||||
-rw-r--r-- | src/search.cpp | 1 | ||||
-rw-r--r-- | src/string-utils.cpp | 51 | ||||
-rw-r--r-- | src/string-utils.h | 96 |
11 files changed, 154 insertions, 72 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 1819c19..c1a1c7d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,7 @@ EXTRA_DIST = gtk-info-popup.gob \ noinst_LIBRARIES = libsciteco-base.a libsciteco_base_a_SOURCES = main.cpp sciteco.h \ + string-utils.cpp string-utils.h \ error.cpp error.h \ cmdline.cpp cmdline.h \ undo.cpp undo.h \ diff --git a/src/cmdline.cpp b/src/cmdline.cpp index f227053..7c74db4 100644 --- a/src/cmdline.cpp +++ b/src/cmdline.cpp @@ -27,6 +27,7 @@ #include <glib/gstdio.h> #include "sciteco.h" +#include "string-utils.h" #include "interface.h" #include "expressions.h" #include "parser.h" diff --git a/src/error.h b/src/error.h index 4a18afb..7ee0ac6 100644 --- a/src/error.h +++ b/src/error.h @@ -25,6 +25,7 @@ #include <glib/gprintf.h> #include "sciteco.h" +#include "string-utils.h" namespace SciTECO { diff --git a/src/goto.cpp b/src/goto.cpp index c8cb33e..9216330 100644 --- a/src/goto.cpp +++ b/src/goto.cpp @@ -23,6 +23,7 @@ #include <glib/gprintf.h> #include "sciteco.h" +#include "string-utils.h" #include "expressions.h" #include "parser.h" #include "undo.h" diff --git a/src/main.cpp b/src/main.cpp index d7b9cc0..126e360 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,29 +81,6 @@ static gpointer g_realloc_exception(gpointer mem, gsize n_bytes) static void sigint_handler(int signal); } -void -String::get_coord(const gchar *str, gint pos, - gint &line, gint &column) -{ - line = column = 1; - - for (gint i = 0; i < pos; i++) { - switch (str[i]) { - case '\r': - if (str[i+1] == '\n') - i++; - /* fall through */ - case '\n': - line++; - column = 1; - break; - default: - column++; - break; - } - } -} - #ifdef G_OS_WIN32 /* diff --git a/src/parser.cpp b/src/parser.cpp index 97876b0..e84edb5 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -27,6 +27,7 @@ #include <glib/gstdio.h> #include "sciteco.h" +#include "string-utils.h" #include "interface.h" #include "undo.h" #include "expressions.h" diff --git a/src/qregisters.cpp b/src/qregisters.cpp index 64d80e8..9cda9ca 100644 --- a/src/qregisters.cpp +++ b/src/qregisters.cpp @@ -29,6 +29,7 @@ #include <Scintilla.h> #include "sciteco.h" +#include "string-utils.h" #include "interface.h" #include "undo.h" #include "parser.h" diff --git a/src/sciteco.h b/src/sciteco.h index 4830312..53131dc 100644 --- a/src/sciteco.h +++ b/src/sciteco.h @@ -66,55 +66,6 @@ extern sig_atomic_t sigint_occurred; #define IS_SUCCESS(X) ((X) < 0) #define IS_FAILURE(X) (!IS_SUCCESS(X)) -namespace String { - -static inline gchar * -chrdup(gchar chr) -{ - gchar *ret = (gchar *)g_malloc(2); - - /* - * NOTE: even the glib allocs are configured to throw exceptions, - * so there is no error handling necessary - */ - ret[0] = chr; - ret[1] = '\0'; - return ret; -} - -static inline void -append(gchar *&str1, const gchar *str2) -{ - /* FIXME: optimize */ - gchar *new_str = g_strconcat(str1 ? : "", str2, NIL); - g_free(str1); - str1 = new_str; -} - -static inline void -append(gchar *&str, gchar chr) -{ - gchar buf[] = {chr, '\0'}; - append(str, buf); -} - -/* in main.cpp */ -void get_coord(const gchar *str, gint pos, - gint &line, gint &column); - -static inline gsize -diff(const gchar *a, const gchar *b) -{ - gsize len = 0; - - while (*a != '\0' && *a++ == *b++) - len++; - - return len; -} - -} /* namespace String */ - namespace Validate { static inline bool diff --git a/src/search.cpp b/src/search.cpp index 3d6b042..9519fb9 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -25,6 +25,7 @@ #include <glib/gprintf.h> #include "sciteco.h" +#include "string-utils.h" #include "expressions.h" #include "undo.h" #include "qregisters.h" diff --git a/src/string-utils.cpp b/src/string-utils.cpp new file mode 100644 index 0000000..6481765 --- /dev/null +++ b/src/string-utils.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2012-2015 Robin Haberkorn + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <glib.h> + +#include "string-utils.h" + +namespace SciTECO { + +void +String::get_coord(const gchar *str, gint pos, + gint &line, gint &column) +{ + line = column = 1; + + for (gint i = 0; i < pos; i++) { + switch (str[i]) { + case '\r': + if (str[i+1] == '\n') + i++; + /* fall through */ + case '\n': + line++; + column = 1; + break; + default: + column++; + break; + } + } +} + +} /* namespace SciTECO */ diff --git a/src/string-utils.h b/src/string-utils.h new file mode 100644 index 0000000..e2f7be3 --- /dev/null +++ b/src/string-utils.h @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2012-2015 Robin Haberkorn + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __STRING_UTILS_H +#define __STRING_UTILS_H + +#include <string.h> + +#include <glib.h> + +namespace SciTECO { + +namespace String { + +/** + * Allocate a string containing a single character chr. + */ +static inline gchar * +chrdup(gchar chr) +{ + gchar *ret = (gchar *)g_malloc(2); + + ret[0] = chr; + ret[1] = '\0'; + + return ret; +} + +/** + * Append null-terminated str2 to non-null-terminated + * str1 of length str1_size. + * The result is not null-terminated. + */ +static inline void +append(gchar *&str1, gsize str1_size, const gchar *str2) +{ + size_t str2_size = strlen(str2); + str1 = (gchar *)g_realloc(str1, str1_size + str2_size); + if (str1) + memcpy(str1+str1_size, str2, str2_size); +} + +/** + * Append str2 to str1 (both null-terminated). + */ +static inline void +append(gchar *&str1, const gchar *str2) +{ + size_t str1_size = str1 ? strlen(str1) : 0; + str1 = (gchar *)g_realloc(str1, str1_size + strlen(str2) + 1); + strcpy(str1+str1_size, str2); +} + +/** + * Append a single character to a null-terminated string. + */ +static inline void +append(gchar *&str, gchar chr) +{ + gchar buf[] = {chr, '\0'}; + append(str, buf); +} + +void get_coord(const gchar *str, gint pos, + gint &line, gint &column); + +static inline gsize +diff(const gchar *a, const gchar *b) +{ + gsize len = 0; + + while (*a != '\0' && *a++ == *b++) + len++; + + return len; +} + +} /* namespace String */ + +} /* namespace SciTECO */ + +#endif |