Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/base/nsCRTGlue.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsCRTGlue_h__
8
#define nsCRTGlue_h__
9
10
#include "nscore.h"
11
12
/**
13
 * Scan a string for the first character that is *not* in a set of
14
 * delimiters.  If the string is only delimiter characters, the end of the
15
 * string is returned.
16
 *
17
 * @param aDelims The set of delimiters (null-terminated)
18
 * @param aStr    The string to search (null-terminated)
19
 */
20
const char* NS_strspnp(const char* aDelims, const char* aStr);
21
22
/**
23
 * Tokenize a string. This function is similar to the strtok function in the
24
 * C standard library, but it does not use static variables to maintain state
25
 * and is therefore thread and reentrancy-safe.
26
 *
27
 * Any leading delimiters in str are skipped. Then the string is scanned
28
 * until an additional delimiter or end-of-string is found. The final
29
 * delimiter is set to '\0'.
30
 *
31
 * @param aDelims The set of delimiters.
32
 * @param aStr    The string to search. This is an in-out parameter; it is
33
 *                reset to the end of the found token + 1, or to the
34
 *                end-of-string if there are no more tokens.
35
 * @return        The token. If no token is found (the string is only
36
 *                delimiter characters), nullptr is returned.
37
 */
38
char* NS_strtok(const char* aDelims, char** aStr);
39
40
/**
41
 * "strlen" for char16_t strings
42
 */
43
uint32_t NS_strlen(const char16_t* aString);
44
45
/**
46
 * "strcmp" for char16_t strings
47
 */
48
int NS_strcmp(const char16_t* aStrA, const char16_t* aStrB);
49
50
/**
51
 * "strncmp" for char16_t strings
52
 */
53
int NS_strncmp(const char16_t* aStrA, const char16_t* aStrB, size_t aLen);
54
55
/**
56
 * "strdup" for char16_t strings, uses the infallible moz_xmalloc allocator.
57
 */
58
char16_t* NS_xstrdup(const char16_t* aString);
59
60
/**
61
 * "strdup", but using the infallible moz_xmalloc allocator.
62
 */
63
char* NS_xstrdup(const char* aString);
64
65
/**
66
 * strndup for char16_t or char strings (normal strndup is not available on
67
 * windows). This function will ensure that the new string is
68
 * null-terminated. Uses the infallible moz_xmalloc allocator.
69
 *
70
 * CharT may be either char16_t or char.
71
 */
72
template<typename CharT>
73
CharT* NS_xstrndup(const CharT* aString, uint32_t aLen);
74
75
// The following case-conversion methods only deal in the ascii repertoire
76
// A-Z and a-z
77
78
// semi-private data declarations... don't use these directly.
79
class nsLowerUpperUtils
80
{
81
public:
82
  static const unsigned char kLower2Upper[256];
83
  static const unsigned char kUpper2Lower[256];
84
};
85
86
inline char
87
NS_ToUpper(char aChar)
88
0
{
89
0
  return (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
90
0
}
91
92
inline char
93
NS_ToLower(char aChar)
94
{
95
  return (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
96
}
97
98
bool NS_IsUpper(char aChar);
99
bool NS_IsLower(char aChar);
100
101
constexpr bool
102
NS_IsAscii(char16_t aChar)
103
0
{
104
0
  return (0x0080 > aChar);
105
0
}
106
107
constexpr bool
108
NS_IsAscii(const char16_t* aString)
109
0
{
110
0
  while (*aString) {
111
0
    if (0x0080 <= *aString) {
112
0
      return false;
113
0
    }
114
0
    aString++;
115
0
  }
116
0
  return true;
117
0
}
118
119
constexpr bool
120
NS_IsAscii(const char* aString)
121
0
{
122
0
  while (*aString) {
123
0
    if (0x80 & *aString) {
124
0
      return false;
125
0
    }
126
0
    aString++;
127
0
  }
128
0
  return true;
129
0
}
130
131
constexpr bool
132
NS_IsAscii(const char* aString, uint32_t aLength)
133
0
{
134
0
  const char* end = aString + aLength;
135
0
  while (aString < end) {
136
0
    if (0x80 & *aString) {
137
0
      return false;
138
0
    }
139
0
    aString++;
140
0
  }
141
0
  return true;
142
0
}
143
144
constexpr bool
145
NS_IsAsciiWhitespace(char16_t aChar)
146
{
147
  return aChar == ' ' ||
148
         aChar == '\r' ||
149
         aChar == '\n' ||
150
         aChar == '\t';
151
}
152
153
#ifndef XPCOM_GLUE_AVOID_NSPR
154
void NS_MakeRandomString(char* aBuf, int32_t aBufLen);
155
#endif
156
157
#define FF '\f'
158
#define TAB '\t'
159
160
#define CRSTR "\015"
161
#define LFSTR "\012"
162
#define CRLF "\015\012"     /* A CR LF equivalent string */
163
164
// We use the most restrictive filesystem as our default set of illegal filename
165
// characters. This is currently Windows.
166
#define OS_FILE_ILLEGAL_CHARACTERS "/:*?\"<>|"
167
// We also provide a list of all known file path separators for all filesystems.
168
// This can be used in replacement of FILE_PATH_SEPARATOR when you need to
169
// identify or replace all known path separators.
170
#define KNOWN_PATH_SEPARATORS "\\/"
171
172
#if defined(XP_MACOSX)
173
  #define FILE_PATH_SEPARATOR        "/"
174
#elif defined(XP_WIN)
175
  #define FILE_PATH_SEPARATOR        "\\"
176
#elif defined(XP_UNIX)
177
  #define FILE_PATH_SEPARATOR        "/"
178
#else
179
  #error need_to_define_your_file_path_separator_and_maybe_illegal_characters
180
#endif
181
182
// Not all these control characters are illegal in all OSs, but we don't really
183
// want them appearing in filenames
184
#define CONTROL_CHARACTERS     "\001\002\003\004\005\006\007" \
185
                           "\010\011\012\013\014\015\016\017" \
186
                           "\020\021\022\023\024\025\026\027" \
187
                           "\030\031\032\033\034\035\036\037"
188
189
#define FILE_ILLEGAL_CHARACTERS CONTROL_CHARACTERS OS_FILE_ILLEGAL_CHARACTERS
190
191
#endif // nsCRTGlue_h__