str.xml 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
  4. <section id="str" xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <sectioninfo>
  6. <revhistory>
  7. <revision>
  8. <revnumber>$Revision$</revnumber>
  9. <date>$Date$</date>
  10. </revision>
  11. </revhistory>
  12. </sectioninfo>
  13. <title>Type <type>str</type></title>
  14. <para>
  15. One of our main goals was to make <acronym>SER</acronym> really
  16. fast. There are many functions across the server that need to work with
  17. strings. Usually these functions need to know string length. We wanted
  18. to avoid using of <function>strlen</function> because the function is
  19. relatively slow. It must scan the whole string and find the first
  20. occurrence of zero character. To avoid this, we created
  21. <type>str</type> type. The type has 2 fields, field
  22. <structfield>s</structfield> is pointer to the beginning of the string
  23. and field <structfield>len</structfield> is length of the string. We
  24. then calculate length of the string only once and later reuse saved
  25. value.
  26. </para>
  27. <important>
  28. <para>
  29. <type>str</type> structure is quite important because it is widely
  30. used in <acronym>SER</acronym> (most functions accept
  31. <type>str</type> instead of <type>char*</type>).
  32. </para>
  33. </important>
  34. <para><emphasis><type>str</type> Type Declaration</emphasis></para>
  35. <programlisting>
  36. struct _str{
  37. char* s;
  38. int len;
  39. };
  40. typedef struct _str str;
  41. </programlisting>
  42. <para>
  43. The declaration can be found in header file <filename>str.h</filename>.
  44. </para>
  45. <warning>
  46. <para>
  47. Because we store string lengths, there is no need to zero terminate
  48. them. Some strings in the server are still zero terminated, some
  49. are not. Be careful when using functions like
  50. <function>snprintf</function> that rely on the ending zero. You can
  51. print variable of type <type>str</type> this way:
  52. <programlisting>
  53. printf("%.*s", mystring->len, mystring->s);
  54. </programlisting>
  55. That ensures that the string will be printed correctly even if
  56. there is no zero character at the end.
  57. </para>
  58. </warning>
  59. </section>