1
/* Copyright 2016 Software Freedom Conservancy Inc.
3
* This software is licensed under the GNU Lesser General Public License
4
* (version 2.1 or later). See the COPYING file in this distribution.
7
namespace Geary.Imap.DataFormat {
9
private const char[] ATOM_SPECIALS = {
10
'(', ')', '{', ' ', // CTL chars are handled by is_special_char
11
'%', '*', // list-wildcards
12
'"', '\\', // quoted-specials
16
private const char[] TAG_SPECIALS = {
17
'(', ')', '{', '%', '\"', '\\', '+'
26
private bool is_special_char(char ch, char[] ar, string? exceptions) {
27
// Check for CTL chars
28
if (ch <= 0x1F || ch >= 0x7F) {
33
return (exceptions != null) ? Ascii.index_of(exceptions, ch) < 0 : true;
40
* Returns true if the character is considered an atom special. Note that while documentation
41
* indicates that the backslash cannot be used in an atom, they *are* used for message flags and
42
* thus must be special cased by the caller.
44
public inline bool is_atom_special(char ch, string? exceptions = null) {
45
return is_special_char(ch, ATOM_SPECIALS, exceptions);
49
* Tag specials are like atom specials but include the continuation character ('+'). Also, the
50
* star character is allowed, although technically only correct in the context of a status response;
51
* it's the responsibility of the caller to catch this.
53
public bool is_tag_special(char ch, string? exceptions = null) {
54
return is_special_char(ch, TAG_SPECIALS, exceptions);
58
* Returns Quoting to indicate if the string must be quoted before sent on the wire, of if it
59
* must be sent as a literal.
61
public Quoting is_quoting_required(string str) {
62
if (String.is_empty(str))
63
return Quoting.REQUIRED;
67
char ch = str[index++];
72
return Quoting.UNALLOWED;
77
return Quoting.UNALLOWED;
80
if (is_atom_special(ch))
81
return Quoting.REQUIRED;
86
return Quoting.OPTIONAL;