types.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #ifndef BDE_TYPES_H
  17. #define BDE_TYPES_H
  18. #include <string>
  19. #include <stdarg.h> // u8string::cformat
  20. #include <fribidi/fribidi.h>
  21. #include "directvect.h"
  22. typedef FriBidiChar unichar;
  23. typedef FriBidiStrIndex idx_t;
  24. class unistring;
  25. class u8string : public std::string {
  26. public:
  27. u8string() { }
  28. u8string(const u8string &s, size_type pos = 0, size_type n = npos)
  29. : std::string(s, pos, n) { }
  30. u8string(const char *s)
  31. : std::string(s) { }
  32. u8string(const char *first, const char *last)
  33. : std::string(first, last) { }
  34. void init_from_unichars(const unichar *src, int len);
  35. void init_from_unichars(const unistring &str);
  36. u8string(const unistring &str) {
  37. init_from_unichars(str);
  38. }
  39. int len() const { return (int)size(); }
  40. void cformat(const char *fmt, ...);
  41. void vcformat(const char *fmt, va_list ap);
  42. void clear() {
  43. erase(begin(), end());
  44. }
  45. int index(char c, int from = 0) const {
  46. size_type pos = find_first_of(c, from);
  47. return pos == npos ? -1 : (int)pos;
  48. }
  49. int rindex(char c) const {
  50. size_type pos = find_last_of(c);
  51. return pos == npos ? -1 : (int)pos;
  52. }
  53. int index(const char *s, int from = 0) const;
  54. u8string substr(int from, int len = -1) const {
  55. if (len == -1 || (from + len > (int)size()))
  56. len = size() - from;
  57. return u8string(&*(begin() + from), &*(begin() + from + len));
  58. }
  59. u8string erase_char(char xch) const;
  60. u8string trim() const;
  61. void inplace_trim();
  62. u8string toupper_ascii() const;
  63. };
  64. // we use "cstring" in the source code when we want to
  65. // emphasize that this is not a UTF-8 encoded string.
  66. #define cstring u8string
  67. class unistring : public DirectVector<unichar> {
  68. public:
  69. unistring() { }
  70. unistring(size_type n)
  71. : DirectVector<unichar>(n) { }
  72. unistring (const unichar *first, const unichar *last) {
  73. insert(end(), first, last);
  74. }
  75. void init_from_utf8(const char *s, int len);
  76. void init_from_utf8(const char *first, const char *last) {
  77. init_from_utf8(first, last - first);
  78. }
  79. void init_from_utf8(const char *s);
  80. unistring(const u8string &u8) {
  81. init_from_utf8(u8.c_str());
  82. }
  83. void init_from_latin1(const char *s);
  84. void init_from_filename(const char *filename);
  85. idx_t len() const { return (idx_t)size(); }
  86. void append(const unichar *s, size_type len) {
  87. insert(end(), s, s + len);
  88. }
  89. void append(const unistring &s, size_type len) {
  90. insert(end(), s.begin(), s.begin() + len);
  91. }
  92. void append(const unistring &s) {
  93. insert(end(), s.begin(), s.end());
  94. }
  95. void erase_head(size_type len) {
  96. erase(begin(), begin() + len);
  97. }
  98. unistring substr(int from, int len = -1) const {
  99. if (len == -1 || (from + len > (int)size()))
  100. len = size() - from;
  101. return unistring(begin() + from, begin() + from + len);
  102. }
  103. unistring toupper_ascii() const;
  104. int index(unichar ch) const;
  105. bool has_char(unichar ch) const;
  106. int index(const unistring &sub, int from = 0) const;
  107. };
  108. #define STREQ(a,b) (strcmp(a, b) == 0)
  109. #endif