문자열 포트

문자열 포트 (String Ports)

문자열 포트는 바이트 문자열에서 읽거나 바이트 문자열로 씁니다. 입력 문자열 포트는 바이트 문자열이나 문자열로 만들 수 있고, 출력 문자열 포트는 출력을 바이트 문자열로 모아서 get-output-string으로 편리하게 문자열로 변환합니다.

출처: Racket Reference

본문

문자열 포트는 바이트 문자열에서 읽거나 바이트 문자열로 씁니다. 입력 문자열 포트는 바이트 문자열이나 문자열로 만들 수 있는데, 후자의 경우 문자열은 string->bytes/utf-8을 사용해 사실상 바이트 문자열로 변환됩니다. 출력 문자열 포트는 출력을 바이트 문자열로 모으지만, get-output-string이 축적된 바이트를 편리하게 문자열로 변환해 줍니다.

입력/출력 문자열 포트는 명시적으로 닫을 필요가 없습니다. file-position 절차는 문자열 포트에 대해 위치 설정 모드에서 동작합니다.

바이트 문자열에 대한 정보는 Byte Strings 문서도 참고하세요.

procedure

(string-port? p) → boolean?
  p : port?

p가 문자열 포트이면 #t를, 그렇지 않으면 #f를 반환합니다.

base 패키지의 6.0.1.6 버전에서 추가되었습니다.

procedure

(open-input-bytes bstr [name]) → (and/c input-port? string-port?)
  bstr : bytes?
  name : any/c = 'string

bstr에서 문자를 읽는 입력 문자열 포트를 만듭니다(Byte Strings 참고). 이후 bstr을 수정해도 포트가 만들어내는 바이트 스트림에는 영향을 주지 않습니다. 선택적 name 인자는 반환된 포트의 이름으로 사용됩니다.

예시:

> (define sp (open-input-bytes #"(apples 42 day)"))
> (define sexp1 (read sp))
> (first sexp1)

'apples
> (rest sexp1)

'(42 day)
> (read-line (open-input-bytes
              #"the cow jumped over the moon\nthe little dog\n"))

"the cow jumped over the moon"

문자열에 대한 정보는 Strings 문서도 참고하세요.

procedure

(open-input-string str [name]) → (and/c input-port? string-port?)
  str : string?
  name : any/c = 'string

str의 UTF-8 인코딩(Encodings and Locales 참고)에서 바이트를 읽는 입력 문자열 포트를 만듭니다. 선택적 name 인자는 반환된 포트의 이름으로 사용됩니다.

예시:

> (define sp (open-input-string "(λ (x) x)"))
> (read sp)

'(λ (x) x)
> (define names (open-input-string "Günter Harder\nFrédéric Paulin\n"))
> (read-line names)

"Günter Harder"
> (read-line names)

"Frédéric Paulin"

procedure

(open-output-bytes [name]) → (and/c output-port? string-port?)
  name : any/c = 'string

출력을 바이트 문자열로 모으는 출력 문자열 포트를 만듭니다. 선택적 name 인자는 반환된 포트의 이름으로 사용됩니다.

예시:

> (define op1 (open-output-bytes))
> (write '((1 2 3) ("Tom" "Dick") ('a 'b 'c)) op1)
> (get-output-bytes op1)

#\"((1 2 3) (\"Tom\" \"Dick\") ((quote a) (quote b) (quote c)))\"
> (define op2 (open-output-bytes))
> (write "Hi " op2)
> (write "there" op2)
> (get-output-bytes op2)

#\"\\\"Hi \\\"\\\"there\\\"\"
> (define op3 (open-output-bytes))
> (write-bytes #"Hi " op3)

3
> (write-bytes #"there" op3)

5
> (get-output-bytes op3)

#\"Hi there\"

procedure

(open-output-string [name]) → (and/c output-port? string-port?)
  name : any/c = 'string

open-output-bytes와 같습니다.

예시:

> (define op1 (open-output-string))
> (write '((1 2 3) ("Tom" "Dick") ('a 'b 'c)) op1)
> (get-output-string op1)

"((1 2 3) (\"Tom\" \"Dick\") ((quote a) (quote b) (quote c)))"
> (define op2 (open-output-string))
> (write "Hi " op2)
> (write "there" op2)
> (get-output-string op2)

"\\\"Hi \\\"\\\"there\\\""
> (define op3 (open-output-string))
> (write-string "Hi " op3)

3
> (write-string "there" op3)

5
> (get-output-string op3)

"Hi there"

procedure

(get-output-bytes out
  [reset?
  start-pos
  end-pos]) → bytes?

  out : (and/c output-port? string-port?)
  reset? : any/c = #f
  start-pos : exact-nonnegative-integer? = 0
  end-pos : exact-nonnegative-integer? = #f

지금까지 문자열 포트 out에 축적된 바이트(포트의 현재 위치 이후에 쓰인 바이트가 있다면 그것도 포함)를 새로 할당된 바이트 문자열로 반환합니다. outopen-output-bytes(또는 open-output-string)가 만든 출력 문자열 포트이거나, 그런 출력 포트를 (전이적으로) 가리키는 prop:output-port 속성을 가진 구조체여야 합니다.

reset?가 참(true)이면, 모든 바이트가 포트에서 제거되고 포트의 위치가 0으로 재설정됩니다. reset?#f이면, 모든 바이트가 포트에 남아 더 축적됩니다(그래서 이후의 get-output-bytesget-output-string 호출에서 반환됩니다), 그리고 포트의 위치는 변하지 않습니다.

start-posend-pos 인자는 포트에서 반환할 바이트의 범위를 지정합니다. start-posend-pos를 제공하는 것은 get-output-bytes의 결과에 subbytes를 사용하는 것과 같지만, get-output-bytes에 제공하면 할당을 피할 수 있습니다. end-pos 인자는 #f일 수 있는데, 이는 subbytes에 두 번째 인자를 전달하지 않는 것에 해당합니다.

예시:

> (define op (open-output-bytes))
> (write '((1 2 3) ("Tom" "Dick") ('a 'b 'c)) op)
> (get-output-bytes op)

#\"((1 2 3) (\"Tom\" \"Dick\") ((quote a) (quote b) (quote c)))\"
> (get-output-bytes op #f 3 16)

#\" 2 3) (\\\"Tom\\\" \"
> (get-output-bytes op #t)

#\"((1 2 3) (\"Tom\" \"Dick\") ((quote a) (quote b) (quote c)))\"
> (get-output-bytes op)

#\"\"

procedure

(get-output-string out) → string?
  out : (and/c output-port? string-port?)

(bytes->string/utf-8 (get-output-bytes out) #\uFFFD)를 반환합니다.

예시:

> (define i (open-input-string "hello world"))
> (define o (open-output-string))
> (write (read i) o)
> (get-output-string o)

"hello"

더 알아보기

  • Byte Strings 관련 문서
  • Encodings and Locales 관련 문서
  • file-position, subbytes 관련 문서