BRL-CAD
op.c
Go to the documentation of this file.
1 /* O P . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 /* interface header */
24 #include "rt/op.h"
25 
26 /* implementation headers */
27 #include <ctype.h>
28 
29 
30 db_op_t
31 db_str2op(const char *str)
32 {
33  db_op_t ret = DB_OP_NULL;
34 
35  if (!str)
36  return ret;
37 
38  /* skip any leading whitespace */
39  while (*str && isspace((int)(*str)))
40  str++;
41 
42  if (str[0] == '\0')
43  return ret;
44 
45  if (isprint(str[0])) {
46  /* single byte/char */
47  switch (str[0]) {
48  case 'u':
49  ret = DB_OP_UNION;
50  break;
51  case '-':
52  case '\\':
53  ret = DB_OP_SUBTRACT;
54  break;
55  case '+':
56  case 'n':
57  case 'x':
58  ret = DB_OP_INTERSECT;
59  break;
60  }
61  return ret;
62  }
63 
64  /* check if multibyte */
65 
66  if (((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x88 && (unsigned char)str[2] == 0xAA) /* union symbol */
67  || ((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x8B && (unsigned char)str[2] == 0x83) /* n-ary union */
68  )
69  {
70  ret = DB_OP_UNION;
71  } else if (((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x80 && (unsigned char)str[2] > 0x89 && (unsigned char)str[2] < 0x96)
72  /* first check matches unicode symbol variants starting
73  * with hyphen, non-breaking hypen, figure dash, en dash,
74  * em dash, and horizontal bar.
75  */
76  || ((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x88 && (unsigned char)str[2] == 0x92) /* minus sign */
77  || ((unsigned char)str[0] == 0xEF && (unsigned char)str[1] == 0xB9 && (unsigned char)str[2] == 0x98) /* small em dash */
78  || ((unsigned char)str[0] == 0xEF && (unsigned char)str[1] == 0xB9 && (unsigned char)str[2] == 0xA3) /* small hypen minus */
79  || ((unsigned char)str[0] == 0xEF && (unsigned char)str[1] == 0xBC && (unsigned char)str[2] == 0x8D) /* fullwidth hyphen-minus */
80  || ((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x88 && (unsigned char)str[2] == 0x96) /* set minus */
81  || ((unsigned char)str[0] == 0xCB && (unsigned char)str[1] == 0x97) /* utf-16, modifier minus sign */
82  )
83  {
84  ret = DB_OP_SUBTRACT;
85  } else if (((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x88 && (unsigned char)str[2] == 0xA9) /* intersection symbol */
86  || ((unsigned char)str[0] == 0xE2 && (unsigned char)str[1] == 0x8B && (unsigned char)str[2] == 0x82) /* n-ary intersection */
87  || ((unsigned char)str[0] == 0xCB && (unsigned char)str[1] == 0x96) /* utf-16, modifier plus sign */
88  || ((unsigned char)str[0] == 0xC3 && (unsigned char)str[1] == 0x97) /* utf-16, multiplication sign */
89  )
90  {
91  ret = DB_OP_INTERSECT;
92  }
93 
94  return ret;
95 }
96 
97 
98 /*
99  * Local Variables:
100  * tab-width: 8
101  * mode: C
102  * indent-tabs-mode: t
103  * c-file-style: "stroustrup"
104  * End:
105  * ex: shiftwidth=4 tabstop=8
106  */
Header file for the BRL-CAD common definitions.
db_op_t
Definition: op.h:33
Definition: op.h:35
Definition: op.h:34
db_op_t db_str2op(const char *str)
Definition: op.c:31