Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 : * (initial developer) ]
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #ifndef INCLUDED_L10NTOOLS_SOURCE_HELPER_HXX
31 : #define INCLUDED_L10NTOOLS_SOURCE_HELPER_HXX
32 :
33 : #include "sal/config.h"
34 :
35 : #include <algorithm>
36 : #include <cassert>
37 :
38 : #include <libxml/parser.h>
39 :
40 : #include "rtl/string.hxx"
41 : #include "rtl/ustring.hxx"
42 : #include "rtl/strbuf.hxx"
43 : #include "sal/types.h"
44 :
45 : namespace helper {
46 :
47 : // cf. comphelper::string::isdigitAsciiString:
48 : inline bool isAllAsciiDigits(rtl::OString const & text) {
49 : for (sal_Int32 i = 0; i != text.getLength(); ++i) {
50 : if (text[i] < '0' || text[i] > '9') {
51 : return false;
52 : }
53 : }
54 : return true;
55 : }
56 :
57 : // cf. comphelper::string::isupperAsciiString:
58 : inline bool isAllAsciiUpperCase(rtl::OString const & text) {
59 : for (sal_Int32 i = 0; i != text.getLength(); ++i) {
60 : if (text[i] < 'A' || text[i] > 'Z') {
61 : return false;
62 : }
63 : }
64 : return true;
65 : }
66 :
67 : // cf. comphelper::string::islowerAsciiString:
68 : inline bool isAllAsciiLowerCase(rtl::OString const & text) {
69 : for (sal_Int32 i = 0; i != text.getLength(); ++i) {
70 : if (text[i] < 'a' || text[i] > 'z') {
71 : return false;
72 : }
73 : }
74 : return true;
75 : }
76 :
77 1313 : inline sal_Int32 countOccurrences(rtl::OString const & text, char c) {
78 1313 : sal_Int32 n = 0;
79 3939 : for (sal_Int32 i = 0;; ++i) {
80 3939 : i = text.indexOf(c, i);
81 3939 : if (i == -1) {
82 1313 : break;
83 : }
84 2626 : ++n;
85 : }
86 1313 : return n;
87 : }
88 :
89 : inline sal_Int32 indexOfAnyAsciiL(
90 : rtl::OUString const & text, char const * chars, sal_Int32 charsLen,
91 : sal_Int32 index = 0)
92 : {
93 : for (; index != text.getLength(); ++index) {
94 : sal_Unicode c = text[index];
95 : if (c <= 0x7F
96 : && (rtl_str_indexOfChar_WithLength(
97 : chars, charsLen, static_cast< char >(c))
98 : != -1))
99 : {
100 : return index;
101 : }
102 : }
103 : return -1;
104 : }
105 :
106 0 : rtl::OString QuotHTML(const rtl::OString &rString)
107 : {
108 0 : rtl::OStringBuffer sReturn;
109 0 : for (sal_Int32 i = 0; i < rString.getLength(); ++i) {
110 0 : switch (rString[i]) {
111 : case '\\':
112 0 : if (i < rString.getLength()) {
113 0 : switch (rString[i + 1]) {
114 : case '"':
115 : case '<':
116 : case '>':
117 : case '\\':
118 0 : ++i;
119 0 : break;
120 : }
121 : }
122 : // fall through
123 : default:
124 0 : sReturn.append(rString[i]);
125 0 : break;
126 :
127 : case '<':
128 0 : sReturn.append("<");
129 0 : break;
130 :
131 : case '>':
132 0 : sReturn.append(">");
133 0 : break;
134 :
135 : case '"':
136 0 : sReturn.append(""");
137 0 : break;
138 :
139 : case '&':
140 0 : if (rString.matchL(RTL_CONSTASCII_STRINGPARAM("&"), i))
141 0 : sReturn.append('&');
142 : else
143 0 : sReturn.append(RTL_CONSTASCII_STRINGPARAM("&"));
144 0 : break;
145 : }
146 : }
147 0 : return sReturn.makeStringAndClear();
148 : }
149 :
150 0 : inline bool isWellFormedXML( OString const & text )
151 : {
152 : xmlDocPtr doc;
153 0 : OString content;
154 0 : bool result = true;
155 :
156 0 : content = "<root>";
157 0 : content += text;
158 0 : content += "</root>";
159 0 : doc = xmlParseMemory(content.getStr(),(int)content.getLength());
160 0 : if (doc == NULL) {
161 0 : result = false;
162 : }
163 0 : xmlFreeDoc(doc);
164 0 : xmlCleanupParser();
165 0 : return result;
166 : }
167 :
168 :
169 : template< typename T > inline T abbreviate(
170 : T const & text, sal_Int32 start, sal_Int32 length)
171 : {
172 : start = std::max(sal_Int32(0), start);
173 : assert(start <= text.getLength());
174 : return text.copy(start, std::min(text.getLength() - start, length));
175 : }
176 :
177 : }
178 :
179 : #endif
180 :
181 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|