The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

XmlString.vala in libbirdxml

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdxml/XmlString.vala.
Create new paths in pen tool
1 /* 2 Copyright (C) 2014 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 namespace Bird { 15 16 /** 17 * Representation of a string in the XmlParser. This class adds reference counting and 18 * copies a pointer to string instead of the data. It is faster if the string is 19 * huge. 20 */ 21 public class XmlString : GLib.Object { 22 23 public int length; 24 public char* data; 25 public int refcount = 1; 26 27 internal XmlString (char* data, int length) { 28 this.data = data; 29 this.length = length; 30 } 31 32 internal int index_of (string needle, int offset = 0) { 33 int len = length; 34 int needle_len = needle.length; 35 char* needle_data = (char*) needle; 36 char* haystack = data + offset; 37 38 if (needle_len == 0 || offset > length) { 39 return -1; 40 } 41 42 for (int i = 0; i < len; i++) { 43 if (haystack[i] == '\0') { 44 return -1; 45 } 46 47 for (int j = 0; j <= needle_len && i + j < len; j++) { 48 if (j == needle_len) { 49 return offset + i; 50 } 51 52 if (needle_data[j] != haystack[i + j]) { 53 break; 54 } 55 } 56 } 57 58 return -1; 59 } 60 61 internal bool has_prefix (string prefix) { 62 unowned string s = (string) data; 63 bool p = s.has_prefix (prefix); 64 65 if (!p) { 66 return false; 67 } 68 69 return length > prefix.length; 70 } 71 72 internal bool has_suffix (string suffix) { 73 int suffix_length = suffix.length; 74 75 if (length < suffix_length) { 76 return false; 77 } 78 79 return Posix.strncmp ((string) (data + length - suffix_length), suffix, suffix_length) == 0; 80 } 81 82 internal bool get_next_char (ref int index, out unichar c) { 83 unowned string s; 84 unowned string? n = (string) data; 85 86 if (n == null) { 87 c = '\0'; 88 return false; 89 } 90 91 if (index < 0) { 92 c = '\0'; 93 return false; 94 } 95 96 if (index >= length) { 97 c = '\0'; 98 return false; 99 } 100 101 s = (!) n; 102 103 return s.get_next_char (ref index, out c); 104 } 105 106 internal XmlString substring (int offset, int len = -1) { 107 Posix.assert (offset >= 0); 108 Posix.assert (offset < length); 109 110 if (len == -1) { 111 return new XmlString (data + offset, length - offset); 112 } 113 114 Posix.assert (len + offset < length); 115 return new XmlString (data + offset, len); 116 } 117 118 public string to_string () { 119 unowned string s = (string) data; 120 121 if (length == 0) { 122 return "".dup (); 123 } 124 125 return s.ndup (length); 126 } 127 } 128 129 } 130