summaryrefslogtreecommitdiff
path: root/Examples/test-suite/li_std_wstring.i
blob: 88f80beb4a623f5e4fb8cb06dd92866b294ed5cd (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
143
144
%module li_std_wstring

// The languages below are yet to provide std_wstring.i
#if !(defined(SWIGD) || defined(SWIGGO) || defined(SWIGGUILE) || defined(SWIGJAVASCRIPT) || defined(SWIGLUA) || defined(SWIGMZSCHEME) || defined(SWIGOCAML) || defined(SWIGOCTAVE) || defined(SWIGPERL) || defined(SWIGPHP) || defined(SWIGR) || defined(SWIGSCILAB))

%warnfilter(SWIGWARN_TYPEMAP_WCHARLEAK_MSG) wchar_t_const_ptr_member;  // Setting a const wchar_t * variable may leak memory.

%include <std_wstring.i>


// throw is invalid in C++17 and later, only SWIG to use it
#define TESTCASE_THROW1(T1) throw(T1)
%{
#define TESTCASE_THROW1(T1)
%}

%{
// Unicode strings are stored in li_std_wstring.h file which has the BOM appropriately set, primarily for Visual C++ to correctly interpret the wide strings
#include "li_std_wstring.h"
%}

%inline %{
#include <string>
#include <iostream>

bool trace = false;

void show_wstring_bytes(const std::wstring &s) {
  unsigned char *p = (unsigned char *)s.data();
  size_t len = s.size()*sizeof(wchar_t);
  std::wcout << L"s: " << /*s <<*/ L"[";
  for (size_t i = 0; i<len; i++) {
    std::wcout << std::hex << *p << L" ";
    p++;
  }
  std::wcout << L"]" << std::endl;
  std::wcout << std::flush;
}

wchar_t test_wcvalue(wchar_t x) {
  return x;
}

const wchar_t* test_ccvalue(const wchar_t* x) {
  return x;
}

wchar_t* test_cvalue(wchar_t* x) {
  return x;
}
  

wchar_t* test_wchar_overload() {
  return 0;
}

wchar_t* test_wchar_overload(wchar_t *x) {
  return x;
}

std::wstring test_value(std::wstring x) {
  if (trace) {
    std::wcout << "received(C++): " /*<< x */<< std::endl;
    show_wstring_bytes(x);
  }
  return x;
}

const std::wstring& test_const_reference(const std::wstring &x) {
  return x;
}

void test_pointer(std::wstring *x) {
}

void test_const_pointer(const std::wstring *x) {
}

void test_reference(std::wstring &x) {
}

bool test_equal(const wchar_t *wcs, const std::wstring& s) {
  if (trace) {
    show_wstring_bytes(wcs);
    show_wstring_bytes(s);
  }
  return wcs == s;
}

bool test_equal_abc(const std::wstring &s) {
  return test_equal(L"abc", s);
}

bool test_equal_jp(const std::wstring &s) {
  return test_equal(JP_WSTRING, s);
}

bool test_equal_de(const std::wstring &s) {
  return test_equal(DE_WSTRING, s);
}

bool test_equal_ru(const std::wstring &s) {
  return test_equal(RU_WSTRING, s);
}

void test_throw() TESTCASE_THROW1(std::wstring) {
  static std::wstring x = L"throwing test_throw";
  throw x;
}

void test_throw_wchar_t_ptr() TESTCASE_THROW1(std::wstring) {
  static std::wstring x = JP_WSTRING;
  throw x;
}

void test_throw_jp() TESTCASE_THROW1(std::wstring) {
  static std::wstring x = JP_WSTRING;
  throw x;
}

void test_throw_ref_jp() TESTCASE_THROW1(const std::wstring&) {
  static std::wstring x = JP_WSTRING;
  throw x;
}

const char * non_utf8_c_str() {
  return "h\xe9llo";
}

size_t size_wstring(const std::wstring& s) {
  return s.size();
}

struct wchar_test_struct {
  wchar_t wchar_t_member;
  wchar_t* wchar_t_ptr_member;
  const wchar_t* wchar_t_const_ptr_member;
  wchar_test_struct() : wchar_t_member(), wchar_t_ptr_member(), wchar_t_const_ptr_member() {}
};

%}

#endif