1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| parentheses reverseString2 offset |
parentheses := '({[)}]'.
offset := parentheses size // 2.
reverseString2 := [:str |
    | marks |
    marks := OrderedCollection new.
    str copy doWithIndex: [:chr :idx |
        | type |
        type := parentheses indexOf: chr.
        type > offset ifFalse: [type > 0 ifTrue: [marks add: idx; add: type]] ifTrue: [
            (marks notEmpty and: [marks removeLast + offset = type])
                ifTrue: [str swap: idx with: marks removeLast]
                ifFalse: [marks removeLast]]].
    str reversed].

reverseString2 value: '文字列(もじれつ)の反転(はんてん)'.
   "=> '(んてんは)転反の(つれじも)列字文' "
reverseString2 value: '対応[の{とれている(さまざまな)括弧}の(例)]です。'.
   "=> '。すで[(例)の{弧括(なまざまさ)るいてれと}の]応対' "
reverseString2 value: 'これ(は(対応のとれていない)括弧がある例です。'.
   "=> '。すで例るあが弧括(いないてれとの応対)は(れこ' "
reverseString2 value: 'これ(も{対応の)とれていない}括弧の例です。'.
   "=> '。すで例の弧括}いないてれと)の応対{も(れこ' "