home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_include_stringobject.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-26  |  4.5 KB  |  129 lines

  1.  
  2. /* String object interface */
  3.  
  4. #ifndef Py_STRINGOBJECT_H
  5. #define Py_STRINGOBJECT_H
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9.  
  10. /*
  11. Type PyStringObject represents a character string.  An extra zero byte is
  12. reserved at the end to ensure it is zero-terminated, but a size is
  13. present so strings with null bytes in them can be represented.  This
  14. is an immutable object type.
  15.  
  16. There are functions to create new string objects, to test
  17. an object for string-ness, and to get the
  18. string value.  The latter function returns a null pointer
  19. if the object is not of the proper type.
  20. There is a variant that takes an explicit size as well as a
  21. variant that assumes a zero-terminated string.  Note that none of the
  22. functions should be applied to nil objects.
  23. */
  24.  
  25. /* Two speedup hacks.  Caching the hash saves recalculation of a
  26.    string's hash value.  Interning strings (which requires hash
  27.    caching) tries to ensure that only one string object with a given
  28.    value exists, so equality tests are one pointer comparison.
  29.    Together, these can speed the interpreter up by as much as 20%.
  30.    Each costs the size of a long or pointer per string object.  In
  31.    addition, interned strings live until the end of times.  If you are
  32.    concerned about memory footprint, simply comment the #define out
  33.    here (and rebuild everything!). */
  34. #define CACHE_HASH
  35. #ifdef CACHE_HASH
  36. #define INTERN_STRINGS
  37. #endif
  38.  
  39. typedef struct {
  40.     PyObject_VAR_HEAD
  41. #ifdef CACHE_HASH
  42.     long ob_shash;
  43. #endif
  44. #ifdef INTERN_STRINGS
  45.     PyObject *ob_sinterned;
  46. #endif
  47.     char ob_sval[1];
  48. } PyStringObject;
  49.  
  50. extern DL_IMPORT(PyTypeObject) PyString_Type;
  51.  
  52. #define PyString_Check(op) ((op)->ob_type == &PyString_Type)
  53.  
  54. extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int);
  55. extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
  56. extern DL_IMPORT(int) PyString_Size(PyObject *);
  57. extern DL_IMPORT(char *) PyString_AsString(PyObject *);
  58. extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);
  59. extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *);
  60. extern DL_IMPORT(int) _PyString_Resize(PyObject **, int);
  61. extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *);
  62. extern DL_IMPORT(PyObject *) _PyString_FormatLong(PyObject*, int, int,
  63.                           int, char**, int*);
  64.  
  65. #ifdef INTERN_STRINGS
  66. extern DL_IMPORT(void) PyString_InternInPlace(PyObject **);
  67. extern DL_IMPORT(PyObject *) PyString_InternFromString(const char *);
  68. extern DL_IMPORT(void) _Py_ReleaseInternedStrings(void);
  69. #else
  70. #define PyString_InternInPlace(p)
  71. #define PyString_InternFromString(cp) PyString_FromString(cp)
  72. #define _Py_ReleaseInternedStrings()
  73. #endif
  74.  
  75. /* Macro, trading safety for speed */
  76. #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
  77. #define PyString_GET_SIZE(op)  (((PyStringObject *)(op))->ob_size)
  78.  
  79. /* --- Generic Codecs ----------------------------------------------------- */
  80.  
  81. /* Create a string object by decoding the encoded string s of the
  82.    given size. */
  83.  
  84. extern DL_IMPORT(PyObject*) PyString_Decode(
  85.     const char *s,              /* encoded string */
  86.     int size,                   /* size of buffer */
  87.     const char *encoding,       /* encoding */
  88.     const char *errors          /* error handling */
  89.     );
  90.  
  91. /* Encodes a char buffer of the given size and returns a 
  92.    Python string object. */
  93.  
  94. extern DL_IMPORT(PyObject*) PyString_Encode(
  95.     const char *s,              /* string char buffer */
  96.     int size,                   /* number of chars to encode */
  97.     const char *encoding,       /* encoding */
  98.     const char *errors          /* error handling */
  99.     );
  100.  
  101. /* Encodes a string object and returns the result as Python string
  102.    object. */
  103.  
  104. extern DL_IMPORT(PyObject*) PyString_AsEncodedString(
  105.     PyObject *str,         /* string object */
  106.     const char *encoding,    /* encoding */
  107.     const char *errors        /* error handling */
  108.     );
  109.  
  110. /* Provides access to the internal data buffer and size of a string
  111.    object or the default encoded version of an Unicode object. Passing
  112.    NULL as *len parameter will force the string buffer to be
  113.    0-terminated (passing a string with embedded NULL characters will
  114.    cause an exception).  */
  115.  
  116. extern DL_IMPORT(int) PyString_AsStringAndSize(
  117.     register PyObject *obj,    /* string or Unicode object */
  118.     register char **s,        /* pointer to buffer variable */
  119.     register int *len        /* pointer to length variable or NULL
  120.                    (only possible for 0-terminated
  121.                    strings) */
  122.     );
  123.     
  124.  
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif /* !Py_STRINGOBJECT_H */
  129.