Concurrent Clean + 日本語 : マウス操作(位置指定)
この関数が、画面上のポイントからカーソルの位置を計算する部分です。中を見ると、カーソルの位置をバイナリサーチしているようです。このやり方だったら、単なる割り算でよさそうなものですが、タブに対応するために検索が必要になったのでしょう。で、なるべく速度重視で検索をするためにバイナリサーチにしているのではないかと思います。でも、日本語ShiftJISに対応するには、行頭からの全検索にしないと駄目ですね。
< EdVisualText.icl > pointToPosition :: !Point2 !Text !FontInfo -> Position pointToPosition { x, y } text fontInfo # row = y / fontInfo.lineHeight # lastLineNr = textLength text - 1 | (row > lastLineNr) # (lastLine, _) = getLine lastLineNr text = {col=size lastLine, row=lastLineNr} | (row < 0) = {col=0, row=0} # col = findColumn x row text fontInfo = {col=col, row=row} where findColumn :: Int LineNr Text FontInfo -> Int findColumn x row text fontInfo | x < 0 = 0 # (textLine, _) = getLine row text # len = size textLine # splitLine = splitAtTabs textLine # width = tabStringWidth 0 splitLine fontInfo | x > width = len = binarySearch x 0 len splitLine where binarySearch :: Int Int Int [String] -> Int binarySearch x left right splitLine | left==right = left # middle = left + (right - left) / 2 # width1 = tabStringWidth 0 (tabTake middle splitLine) fontInfo # width2 = tabStringWidth 0 (tabTake (middle+1) splitLine) fontInfo # middleX = (width1 + width2) / 2 | x < middleX = binarySearch x left middle splitLine = binarySearch x (middle+1) right splitLine