/collects/scribblings/reference/string-output.scrbl

http://github.com/gmarceau/PLT · Racket · 154 lines · 121 code · 33 blank · 0 comment · 3 complexity · 4d70137ce391493e00ddd1e35fb9e566 MD5 · raw file

  1. #lang scribble/doc
  2. @(require "mz.rkt")
  3. @title{Byte and String Output}
  4. @defproc[(write-char [char character?] [out output-port? (current-output-port)])
  5. void?]{
  6. Writes a single character to @racket[out]; more precisely, the bytes
  7. that are the UTF-8 encoding of @racket[char] are written to
  8. @racket[out].}
  9. @defproc[(write-byte [byte any/c] [out output-port? (current-output-port)])
  10. void?]{
  11. Writes a single byte to @racket[out].}
  12. @defproc[(newline [out output-port? (current-output-port)])
  13. void?]{
  14. The same as @racket[(write-char #\newline out)].}
  15. @defproc[(write-string [str string?]
  16. [out output-port? (current-output-port)]
  17. [start-pos exact-nonnegative-integer? 0]
  18. [end-pos exact-nonnegative-integer? (string-length str)])
  19. exact-nonnegative-integer?]{
  20. Writes characters to @racket[out] from @racket[str] starting from
  21. index @racket[start-pos] (inclusive) up to @racket[end-pos]
  22. (exclusive). Like @racket[substring], the @exnraise[exn:fail:contract]
  23. if @racket[start-pos] or @racket[end-pos] is out-of-range for
  24. @racket[str].
  25. The result is the number of characters written to @racket[out], which
  26. is always @racket[(- end-pos start-pos)].}
  27. @defproc[(write-bytes [bstr bytes?]
  28. [out output-port? (current-output-port)]
  29. [start-pos exact-nonnegative-integer? 0]
  30. [end-pos exact-nonnegative-integer? (bytes-length bstr)])
  31. exact-nonnegative-integer?]{
  32. Like @racket[write-string], but writes bytes instead of characters.}
  33. @defproc[(write-bytes-avail [bstr bytes?]
  34. [out output-port? (current-output-port)]
  35. [start-pos exact-nonnegative-integer? 0]
  36. [end-pos exact-nonnegative-integer? (bytes-length bstr)])
  37. exact-nonnegative-integer?]{
  38. Like @racket[write-bytes], but returns without blocking after writing
  39. as many bytes as it can immediately flush. It blocks only if no bytes
  40. can be flushed immediately. The result is the number of bytes written
  41. and flushed to @racket[out]; if @racket[start-pos] is the same as
  42. @racket[end-pos], then the result can be @racket[0] (indicating a
  43. successful flush of any buffered data), otherwise the result is between
  44. @racket[1] and @racket[(- end-pos
  45. start-pos)], inclusive.
  46. The @racket[write-bytes-avail] procedure never drops bytes; if
  47. @racket[write-bytes-avail] successfully writes some bytes and then
  48. encounters an error, it suppresses the error and returns the number of
  49. written bytes. (The error will be triggered by future writes.) If an
  50. error is encountered before any bytes have been written, an exception
  51. is raised.}
  52. @defproc[(write-bytes-avail* [bstr bytes?]
  53. [out output-port? (current-output-port)]
  54. [start-pos exact-nonnegative-integer? 0]
  55. [end-pos exact-nonnegative-integer? (bytes-length bstr)])
  56. (or/c exact-nonnegative-integer? #f)]{
  57. Like @racket[write-bytes-avail], but never blocks, returns @racket[#f]
  58. if the port contains buffered data that cannot be written immediately,
  59. and returns @racket[0] if the port's internal buffer (if any) is
  60. flushed but no additional bytes can be written immediately.}
  61. @defproc[(write-bytes-avail/enable-break [bstr bytes?]
  62. [out output-port? (current-output-port)]
  63. [start-pos exact-nonnegative-integer? 0]
  64. [end-pos exact-nonnegative-integer? (bytes-length bstr)])
  65. exact-nonnegative-integer?]{
  66. Like @racket[write-bytes-avail], except that breaks are enabled during
  67. the write. The procedure provides a guarantee about the interaction of
  68. writing and breaks: if breaking is disabled when
  69. @racket[write-bytes-avail/enable-break] is called, and if the
  70. @racket[exn:break] exception is raised as a result of the call, then
  71. no bytes will have been written to @racket[out]. See also
  72. @secref["breakhandler"].}
  73. @defproc[(write-special [v any/c] [out output-port? (current-output-port)]) boolean?]{
  74. Writes @racket[v] directly to @racket[out] if the port supports
  75. special writes, or raises @racket[exn:fail:contract] if the port does
  76. not support special write. The result is always @racket[#t],
  77. indicating that the write succeeded.}
  78. @defproc[(write-special-avail* [v any/c] [out output-port? (current-output-port)]) boolean?]{
  79. Like @racket[write-special], but without blocking. If @racket[v]
  80. cannot be written immediately, the result is @racket[#f] without
  81. writing @racket[v], otherwise the result is @racket[#t] and @racket[v]
  82. is written.}
  83. @defproc[(write-bytes-avail-evt [bstr bytes?]
  84. [out output-port? (current-output-port)]
  85. [start-pos exact-nonnegative-integer? 0]
  86. [end-pos exact-nonnegative-integer? (bytes-length bstr)])
  87. evt?]{
  88. Similar to @racket[write-bytes-avail], but instead of writing bytes
  89. immediately, it returns a synchronizable event (see
  90. @secref["sync"]). The @racket[out] must support atomic writes, as
  91. indicated by @racket[port-writes-atomic?].
  92. Synchronizing on the object starts a write from @racket[bstr], and the
  93. event becomes ready when bytes are written (unbuffered) to the
  94. port. If @racket[start-pos] and @racket[end-pos] are the same, then
  95. the synchronization result is @racket[0] when the port's internal
  96. buffer (if any) is flushed, otherwise the result is a positive exact
  97. integer. If the event is not selected in a synchronization, then no
  98. bytes will have been written to @racket[out].}
  99. @defproc[(write-special-evt [v any/c] [out output-port? (current-output-port)]) evt?]{
  100. Similar to @racket[write-special], but instead of writing the special
  101. value immediately, it returns a synchronizable event (see
  102. @secref["sync"]). The @racket[out] must support atomic writes, as
  103. indicated by @racket[port-writes-atomic?].
  104. Synchronizing on the object starts a write of the special value, and
  105. the event becomes ready when the value is written (unbuffered) to the
  106. port. If the event is not selected in a synchronization, then no value
  107. will have been written to @racket[out].}
  108. @defproc[(port-writes-atomic? [out output-port?]) boolean?]{
  109. Returns @racket[#t] if @racket[write-bytes-avail/enable-break] can
  110. provide an exclusive-or guarantee (break or write, but not both) for
  111. @racket[out], and if the port can be used with procedures like
  112. @racket[write-bytes-avail-evt]. Racket's file-stream ports, pipes,
  113. string ports, and TCP ports all support atomic writes; ports created
  114. with @racket[make-output-port] (see @secref["customport"]) may
  115. support atomic writes.}
  116. @defproc[(port-writes-special? [out output-port?]) boolean?]{
  117. Returns @racket[#t] if procedures like @racket[write-special] can
  118. write arbitrary values to the port. Racket's file-stream ports,
  119. pipes, string ports, and TCP ports all reject special values, but
  120. ports created with @racket[make-output-port] (see
  121. @secref["customport"]) may support them.}