2011-02-02 19 views
0

ここは私の状況です。与えられた列と行のchar配列のインデックスを見つける?

私は、幅が大きすぎるか、テキスト内に\ nまたは ' - 'がある場合にテキスト行を生成するワードラッピングアルゴリズムを使用しています。

したがって、テキスト行自体に '\ n'は含まれません。これは私のための挑戦的な部分です。

私は、行と列を指定して、文字配列のインデックスを返すアルゴリズムを作成しようとしています。

したがって、行1の長さが20で行2の長さが10の場合、行1に元々 '\ n'があった場合は、これを考慮する必要があります。

例:

テキストは次のとおりです。私たちは、その後2本のラインを取得

Hello blue sky\nworld 

:私のキャレットが表されている場合、今

Hello blue sky 
world 

を '|'そして私はそれをここに置く:

Hello blue sky 
    |world 

を結果は、14文字ではなく、目に見えない「\ n」は以来、15文字目が考慮されなければなりません。

トリッキーな部分は、ワードラップが発生することを決定した場合である「\をn」の

例:ワードラップは、新しい行をプッシュするので

Hello blue 
    sky 
    |world 

この場合、結果はまだ15でなければなりませんが、テキストに '\ n'があったからではありません。

おかげ

Here is my mess so far: 

int AguiTextBox::indexFromColumnRow(int column, int row) 
{ 
    int len = 0; 
    int charCount = 0; 
    std::string curChar; 

    int curLen = 0; 
    int bytesSkipped = 0; 
    std::string::const_iterator it = getText().begin(); 
    std::string::const_iterator end = getText().end(); 

    if(textRows.size() == 0) 
    { 
     return -1; 
    } 
    for(int i = 0; i < row; ++i) 
    { 
     len = _unicodeFunctions.getUtf8StringLength(textRows[i]); 

     for(int j = 0; j < len; ++j) 
     { 
      //skip characters that would not have passed the newline test 
      do 
      { 
       curLen = _unicodeFunctions.bringToNextUnichar(it,end); 
       curChar = getText().substr(bytesSkipped,curLen); 
       bytesSkipped += curLen; 
       charCount++; 
      } 
      while(curChar[0] < ' '); 
     } 

    } 

    len = len = _unicodeFunctions.getUtf8StringLength(textRows[row]); 


    if(column == 0 && charCount + 1 < getTextLength()) 
    { 
     curChar = _unicodeFunctions.getUtf8SubStr(getText(),charCount,1); 

     while(charCount < getTextLength() - 1) 
     { 
      if(curChar[0] < ' ' && curChar[0] != '\n') 
      { 
       charCount++; 
       curLen = _unicodeFunctions.bringToNextUnichar(it,end); 
       curChar = getText().substr(bytesSkipped,curLen); 
       bytesSkipped += curLen; 
      } 
      else 
      { 
       break; 
      } 
     } 
     if(curChar[0] == '\n') 
     { 
      charCount++; 
      curLen = _unicodeFunctions.bringToNextUnichar(it,end); 
      curChar = getText().substr(bytesSkipped,curLen); 
      bytesSkipped += curLen; 
     } 
    } 
    for (int i = 0; i < column; ++i) 
    { 
     do 
     { 
      curLen = _unicodeFunctions.bringToNextUnichar(it,end); 
      curChar = getText().substr(bytesSkipped,curLen); 
      bytesSkipped += curLen; 
      charCount++; 
     } 
     while(curChar[0] < ' '); 
    } 

    return charCount - 1; 
} 

//and the opposite 
AguiPoint AguiTextBox::columnRowFromIndex(int index) 
{ 
    std::string::const_iterator it = getText().begin(); 
    std::string::const_iterator end = getText().end(); 

    std::string curChar; 
    int charCount = 0; 
    int len = 0; 
    int byteCount = 0; 
    int curLen = 0; 
    for(int i = 0; i < (int)textRows.size(); ++i) 
    { 
     len = _unicodeFunctions.getUtf8StringLength(textRows[i]); 

     for(int j = 0; j < len; ++j) 
     { 

      //skip characters if needed 
      curLen = _unicodeFunctions.bringToNextUnichar(it,end); 
      curChar = getText().substr(byteCount,curLen); 
      byteCount += curLen; 

      while (curChar[0] < ' ' && curChar[0] != '\n') 
      { 
       curLen = _unicodeFunctions.bringToNextUnichar(it,end); 
       curChar = getText().substr(byteCount,curLen); 
       byteCount += curLen; 
      } 
      if(curChar[0] == '\n') 
      { 
       charCount++; 
       if(charCount == index) 
       { 
        return AguiPoint(j,i); 
       } 

      } 
      charCount++; 
      if(charCount == index) 
      { 
       return AguiPoint(j,i); 
      } 



     } 
    } 
    return AguiPoint(len,textRows.size() - 1); 
} 
+0

*コードを*表示します。あなたはこれまで何を持っていて、どこが間違っているのですか? –

+0

@Jonathan Grynspan私は自分のコードを投稿しましたが、うまくいきません。 – jmasterx

+0

'getText()'が値によって 'std :: string'を返すと仮定すると、一度だけ呼び出すべきです。それ以外の場合は、イテレータは正しく一致しません(ただし、実際にどこにでも使用しているとは思われません)。 –

答えて

0

は、それはあなたがデータで何をする必要があるかを正確に私には明らかではありません。あなたはエディタを書こうとしていますか?

あなたができることの1つは、各行の開始点へのポインタの配列と、おそらく各行の長さを作成することです。

このようにして、基礎となるデータを変更せずにすべての行を表すことができます。

しかし、実際に変換されたデータの要件を正確に記述することをお勧めします。

+0

WordWrapをサポートするTextBoxを作成していますが、キャレット位置で削除するにはこの機能が必要です – jmasterx

+0

Windowsではなく、私は推測している?私の提案はうまくいかないでしょうか? –

関連する問題