blob: d30bc03eef5b72cacb2c364f1f7b1ae3417c9862 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_STRINGOBJECT_H
2#define Py_STRINGOBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00008Copyright (c) 2000, BeOpen.com.
9Copyright (c) 1995-2000, Corporation for National Research Initiatives.
10Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
11All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000012
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000013See the file "Misc/COPYRIGHT" for information on usage and
14redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000015******************************************************************/
16
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017/* String object interface */
18
19/*
Guido van Rossumcaa63801995-01-12 11:45:45 +000020Type PyStringObject represents a character string. An extra zero byte is
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021reserved at the end to ensure it is zero-terminated, but a size is
22present so strings with null bytes in them can be represented. This
23is an immutable object type.
24
25There are functions to create new string objects, to test
26an object for string-ness, and to get the
27string value. The latter function returns a null pointer
28if the object is not of the proper type.
29There is a variant that takes an explicit size as well as a
30variant that assumes a zero-terminated string. Note that none of the
31functions should be applied to nil objects.
32*/
33
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000034/* Two speedup hacks. Caching the hash saves recalculation of a
35 string's hash value. Interning strings (which requires hash
36 caching) tries to ensure that only one string object with a given
37 value exists, so equality tests are one pointer comparison.
38 Together, these can speed the interpreter up by as much as 20%.
39 Each costs the size of a long or pointer per string object. In
40 addition, interned strings live until the end of times. If you are
41 concerned about memory footprint, simply comment the #define out
42 here (and rebuild everything!). */
Guido van Rossumfdebf251996-07-30 16:42:03 +000043#define CACHE_HASH
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000044#ifdef CACHE_HASH
45#define INTERN_STRINGS
46#endif
Guido van Rossumfdebf251996-07-30 16:42:03 +000047
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +000049 PyObject_VAR_HEAD
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000050#ifdef CACHE_HASH
51 long ob_shash;
52#endif
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000053#ifdef INTERN_STRINGS
54 PyObject *ob_sinterned;
55#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 char ob_sval[1];
Guido van Rossumcaa63801995-01-12 11:45:45 +000057} PyStringObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058
Guido van Rossum051ab121995-02-27 10:17:52 +000059extern DL_IMPORT(PyTypeObject) PyString_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060
Guido van Rossumcaa63801995-01-12 11:45:45 +000061#define PyString_Check(op) ((op)->ob_type == &PyString_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062
Guido van Rossum43466ec1998-12-04 18:48:25 +000063extern DL_IMPORT(PyObject *) PyString_FromStringAndSize Py_PROTO((const char *, int));
64extern DL_IMPORT(PyObject *) PyString_FromString Py_PROTO((const char *));
65extern DL_IMPORT(int) PyString_Size Py_PROTO((PyObject *));
66extern DL_IMPORT(char *) PyString_AsString Py_PROTO((PyObject *));
67extern DL_IMPORT(void) PyString_Concat Py_PROTO((PyObject **, PyObject *));
68extern DL_IMPORT(void) PyString_ConcatAndDel Py_PROTO((PyObject **, PyObject *));
69extern DL_IMPORT(int) _PyString_Resize Py_PROTO((PyObject **, int));
70extern DL_IMPORT(PyObject *) PyString_Format Py_PROTO((PyObject *, PyObject *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000072#ifdef INTERN_STRINGS
Guido van Rossum43466ec1998-12-04 18:48:25 +000073extern DL_IMPORT(void) PyString_InternInPlace Py_PROTO((PyObject **));
74extern DL_IMPORT(PyObject *) PyString_InternFromString Py_PROTO((const char *));
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000075#else
76#define PyString_InternInPlace(p)
77#define PyString_InternFromString(cp) PyString_FromString(cp)
78#endif
79
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080/* Macro, trading safety for speed */
Barry Warsawaccfb841997-01-06 22:42:50 +000081#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
82#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
Guido van Rossuma3309961993-07-28 09:05:47 +000083
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000084/* --- Generic Codecs ----------------------------------------------------- */
85
86/* Create a string object by decoding the encoded string s of the
87 given size. */
88
89extern DL_IMPORT(PyObject*) PyString_Decode(
90 const char *s, /* encoded string */
91 int size, /* size of buffer */
92 const char *encoding, /* encoding */
93 const char *errors /* error handling */
94 );
95
96/* Encodes a char buffer of the given size and returns a
97 Python string object. */
98
99extern DL_IMPORT(PyObject*) PyString_Encode(
100 const char *s, /* string char buffer */
101 int size, /* number of chars to encode */
102 const char *encoding, /* encoding */
103 const char *errors /* error handling */
104 );
105
106/* Encodes a string object and returns the result as Python string
107 object. */
108
109extern DL_IMPORT(PyObject*) PyString_AsEncodedString(
110 PyObject *str, /* string object */
111 const char *encoding, /* encoding */
112 const char *errors /* error handling */
113 );
114
Guido van Rossuma3309961993-07-28 09:05:47 +0000115#ifdef __cplusplus
116}
117#endif
118#endif /* !Py_STRINGOBJECT_H */