summaryrefslogtreecommitdiffstats
path: root/gettext-tools/src/quote.h
blob: df8cde2df4cb76d30db372c380c79486353426e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Handle quoted segments of a string.
   Copyright (C) 2014-2015 Free Software Foundation, Inc.
   Written by Daiki Ueno <ueno@gnu.org>, 2015.

   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 _QUOTE_H
#define _QUOTE_H

#include <stdbool.h>


#ifdef __cplusplus
extern "C" {
#endif

static void
scan_quoted (const char *input, size_t length,
             void (* callback) (char quote, const char *quoted,
                                size_t quoted_length,
                                void *data),
             void *data)
{
  const char *p, *start, *end;
  bool seen_opening;

  /* START shall point to the beginning of a quoted segment, END
     points to the end of the entire input string.  */
  start = input;
  end = &input[length - 1];
  
  /* True if we have seen a character which could be an opening
     quotation mark.  Note that we can't determine if it is really an
     opening quotation mark until we see a closing quotation mark.  */
  seen_opening = false;

  for (p = start; p <= end; p++)
    {
      switch (*p)
        {
        case '"':
          if (seen_opening)
            {
              if (*start == '"')
                {
                  if (p == start + 1)
                    /* Consider "" as "".  */
                    callback ('\0', "\"\"", 2, data);
                  else
                    /* "..." */
                    callback ('"', start + 1, p - (start + 1), data);

                  start = p + 1;
                  seen_opening = false;
                }
            }
          else
            {
              callback ('\0', start, p - start, data);
              start = p;
              seen_opening = true;
            }
          break;

        case '`':
          if (seen_opening)
            {
              if (*start == '`')
                {
                  callback ('\0', start, p - start, data);
                  start = p;
                }
            }
          else
            {
              callback ('\0', start, p - start, data);
              start = p;
              seen_opening = true;
            }
          break;

        case '\'':
          if (seen_opening)
            {
              if (/* `...' */
                  *start == '`'
                  /* '...', where
                     - The left quote is preceded by a space, and the
                       right quote is followed by a space.
                     - The left quote is preceded by a space, and the
                       right quote is at the end of line.
                     - The left quote is at the beginning of the line, and
                       the right quote is followed by a space.  */
                  || (*start == '\''
                      && (((start > input && *(start - 1) == ' ')
                           && (p == end || *(p + 1) == '\n' || *(p + 1) == ' '))
                          || ((start == input || *(start - 1) == '\n')
                              && p < end && *(p + 1) == ' '))))
                {
                  callback ('\'', start + 1, p - (start + 1), data);
                  start = p + 1;
                }
              else
                {
                  callback ('\0', start, p - start, data);
                  start = p;
                }
              seen_opening = false;
            }
          else if (p == input || *(p - 1) == '\n' || *(p - 1) == ' ')
            {
              callback ('\0', start, p - start, data);
              start = p;
              seen_opening = true;
            }
          break;
        }
    }

  /* Copy the rest.  */
  if (p > start)
    callback ('\0', start, p - start, data);
}


#ifdef __cplusplus
}
#endif


#endif /* _QUOTE_H */