/contrib/groff/src/libs/libgroff/cset.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 112 lines · 77 code · 14 blank · 21 comment · 19 complexity · 18746fcbe3c525f28679f79148c6087b MD5 · raw file

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992, 2004 Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com)
  4. This file is part of groff.
  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING. If not, write to the Free Software
  15. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /* $FreeBSD$ */
  17. #include <ctype.h>
  18. #ifdef __FreeBSD__
  19. #include <locale.h>
  20. #endif
  21. #include "lib.h"
  22. #include "cset.h"
  23. cset csalpha(CSET_BUILTIN);
  24. cset csupper(CSET_BUILTIN);
  25. cset cslower(CSET_BUILTIN);
  26. cset csdigit(CSET_BUILTIN);
  27. cset csxdigit(CSET_BUILTIN);
  28. cset csspace(CSET_BUILTIN);
  29. cset cspunct(CSET_BUILTIN);
  30. cset csalnum(CSET_BUILTIN);
  31. cset csprint(CSET_BUILTIN);
  32. cset csgraph(CSET_BUILTIN);
  33. cset cscntrl(CSET_BUILTIN);
  34. #if defined(isascii) && !defined(__FreeBSD__)
  35. #define ISASCII(c) isascii(c)
  36. #else
  37. #define ISASCII(c) (1)
  38. #endif
  39. void cset::clear()
  40. {
  41. char *p = v;
  42. for (int i = 0; i <= UCHAR_MAX; i++)
  43. p[i] = 0;
  44. }
  45. cset::cset()
  46. {
  47. clear();
  48. }
  49. cset::cset(const char *s)
  50. {
  51. clear();
  52. while (*s)
  53. v[(unsigned char)*s++] = 1;
  54. }
  55. cset::cset(const unsigned char *s)
  56. {
  57. clear();
  58. while (*s)
  59. v[*s++] = 1;
  60. }
  61. cset::cset(cset_builtin)
  62. {
  63. // these are initialised by cset_init::cset_init()
  64. }
  65. cset &cset::operator|=(const cset &cs)
  66. {
  67. for (int i = 0; i <= UCHAR_MAX; i++)
  68. if (cs.v[i])
  69. v[i] = 1;
  70. return *this;
  71. }
  72. int cset_init::initialised = 0;
  73. cset_init::cset_init()
  74. {
  75. if (initialised)
  76. return;
  77. initialised = 1;
  78. #ifdef __FreeBSD__
  79. (void) setlocale(LC_CTYPE, "");
  80. #endif
  81. for (int i = 0; i <= UCHAR_MAX; i++) {
  82. csalpha.v[i] = ISASCII(i) && isalpha(i);
  83. csupper.v[i] = ISASCII(i) && isupper(i);
  84. cslower.v[i] = ISASCII(i) && islower(i);
  85. csdigit.v[i] = ISASCII(i) && isdigit(i);
  86. csxdigit.v[i] = ISASCII(i) && isxdigit(i);
  87. csspace.v[i] = ISASCII(i) && isspace(i);
  88. cspunct.v[i] = ISASCII(i) && ispunct(i);
  89. csalnum.v[i] = ISASCII(i) && isalnum(i);
  90. csprint.v[i] = ISASCII(i) && isprint(i);
  91. csgraph.v[i] = ISASCII(i) && isgraph(i);
  92. cscntrl.v[i] = ISASCII(i) && iscntrl(i);
  93. }
  94. }