forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrType.hh
More file actions
70 lines (58 loc) · 2.03 KB
/
StrType.hh
File metadata and controls
70 lines (58 loc) · 2.03 KB
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
/**
* @file StrType.hh
* @author Caleb Aikens (caleb@distributive.network) & Giovanni Tedesco (giovanni@distributive.network)
* @brief Struct for representing python strings
* @version 0.1
* @date 2022-08-08
*
* @copyright Copyright (c) 2022
*
*/
#ifndef PythonMonkey_StrType_
#define PythonMonkey_StrType_
#include "PyType.hh"
#include "TypeEnum.hh"
#include <jsapi.h>
#include <Python.h>
#include <iostream>
/**
* @brief This struct represents the 'string' type in Python, which is represented as a 'char*' in C++. It inherits from the PyType struct
*/
struct StrType : public PyType {
public:
StrType(PyObject *object);
StrType(char *string);
/**
* @brief Construct a new StrType object from a JSString. Automatically handles encoding conversion for latin1 & UCS2:
* codepoint | Python | Spidermonkey | identical representation?
* 000000-0000FF | latin1 | latin1 | Yes
* 000100-00D7FF | UCS2 | UTF16 | Yes
* 00D800-00DFFF | UCS2 (unpaired) | UTF16 (unpaired) | Yes
* 00E000-00FFFF | UCS2 | UTF16 | Yes
* 010000-10FFFF | UCS4 | UTF16 | No, conversion and new backing store required, user must explicitly call asUCS4()
*
* @param cx - javascript context pointer
* @param str - JSString pointer
*/
StrType(JSContext *cx, JSString *str);
const TYPE returnType = TYPE::STRING;
const char *getValue() const;
/**
* @brief creates new UCS4-encoded pyObject string. This must be called by the user if the original JSString contains any surrogate pairs
*
* @return PyObject* - the UCS4-encoding of the pyObject string
*
*/
PyObject *asUCS4();
protected:
virtual void print(std::ostream &os) const override;
private:
/**
* @brief check if this.pyObject contains a surrogate pair
*
* @return true - pyObject is UCS2-encoded and contains a surrogate pair
* @return false - pyObject is not UCS2-encoded or does not contain a surrogate pair
*/
bool containsSurrogatePair();
};
#endif