「CSV/TSVでアクティブ列選択」の版間の差分

Yuko (トーク | 投稿記録)
編集の要約なし
Yuko (トーク | 投稿記録)
44行目: 44行目:
     }
     }
};
};
outputBar.Clear();


var doc = document;
var doc = document;
138行目: 139行目:
  */
  */
function getDoubleQuoteStartPosition(text, actPos, delimiter) {
function getDoubleQuoteStartPosition(text, actPos, delimiter) {
     for (var i = actPos; i >= 0; i--) {
    var inDoubleQuote = false;
         var checkString = text.charAt(i - 1) + text.charAt(i)
    var fieldStartPos = -1;
         // ダブルクォーテーション範囲閉じ位置に到達したので終了
     for (var i = 0; i < actPos; i++) {
         if (actPos != i && (checkString === '"' + delimiter || checkString === '"\n')) {
         var checkString = text.charAt(i) + text.charAt(i + 1)
             return -1;
        if (!inDoubleQuote && text.charAt(i) === '"' && (text.charAt(i - 1) === "\n" || i === 0)) {
        }
            inDoubleQuote = true;
         // ダブルクォーテーション範囲始まり位置を発見したらその位置を返す
            fieldStartPos = i;
        if (checkString === delimiter + '"' || checkString === '"') {
         } else if (inDoubleQuote && checkString === '""') {
             return i;
            // ダブルクォーテーション内のダブルクォーテーション2つはエスケープされたダブルクォーテーションとして扱うので1文字スキップ
            i++;
         } else if (inDoubleQuote && (checkString === '"' + delimiter || checkString === '"\n')) {
             // ダブルクォーテーション範囲から抜ける
            if (i < actPos - 1) {
                // HACK: カーソル位置が丁度ダブルクォーテーション範囲の終了位置の場合は無視することで、ダブルクォーテーション開始位置を返す
                inDoubleQuote = false;
            }
         } else if (!inDoubleQuote && checkString === delimiter + '"') {
            // ダブルクォーテーション範囲に入る
            inDoubleQuote = true;
            fieldStartPos = i + 1;
             i++;
         }
         }
    }
    if (inDoubleQuote && fieldStartPos === -1) {
        throw new Error("Invalid double quote range.");
    }
    if (inDoubleQuote) {
        return fieldStartPos
     }
     }
     return -1;
     return -1;
167行目: 187行目:
     var fieldCount = 1;
     var fieldCount = 1;
     for (var i = actPos - 1; i >= 0; i--) {
     for (var i = actPos - 1; i >= 0; i--) {
         if (text.charAt(i) === delimiter) {
         var checkString = text.charAt(i - 1) + text.charAt(i)
            var checkString1 = text.charAt(i) + text.charAt(i + 1)
        if (inDoubleQuote && checkString === '""') {
             var checkString2 = text.charAt(i - 1) + text.charAt(i)
            // ダブルクォーテーション内のダブルクォーテーション2つはエスケープされたダブルクォーテーションとして扱うのでスキップ
             i--;
        } else if (inDoubleQuote && checkString === delimiter + '"') {
             // ダブルクォーテーション範囲から抜ける
             // ダブルクォーテーション範囲から抜ける
             if (inDoubleQuote && checkString1 === delimiter + '"') {
             inDoubleQuote = false;
                inDoubleQuote = false;
        } else if (text.charAt(i) === delimiter) {
            }
             // ダブルクォーテーション範囲内のデリミタはカウントしない
             // ダブルクォーテーション範囲内のデリミタはカウントしない
             if (!inDoubleQuote) {
             if (!inDoubleQuote) {
179行目: 200行目:
             }
             }
             // ダブルクォーテーション範囲に入る
             // ダブルクォーテーション範囲に入る
             if (checkString2 === '"' + delimiter) {
             if (checkString === '"' + delimiter) {
                 inDoubleQuote = true;
                 inDoubleQuote = true;
                i--;
             }
             }
         } else if (inDoubleQuote && text.charAt(i) === '"' && (text.charAt(i - 1) === '\n' || i === 0)) {
         } else if (inDoubleQuote && text.charAt(i) === '"' && (text.charAt(i - 1) === '\n' || i === 0)) {
             break;
             break;
         } else if (text.charAt(i) === '\n' && !inDoubleQuote) {
         } else if (!inDoubleQuote && text.charAt(i) === '\n') {
             break;
             break;
         }
         }
233行目: 255行目:
         }
         }
     }
     }
    // 選択範囲位置の追加はしたいが構文解析をスキップしたいときのフラグ
    var skip = false;


     for (var i = 0; i < text.length; i++) {
     for (var i = 0; i < text.length; i++) {
244行目: 269行目:
         }
         }


         var checkString1 = text.charAt(i - 1) + text.charAt(i);
         if (skip) {
         var checkString2 = text.charAt(i) + text.charAt(i + 1);
            skip = false;
         if (text.charAt(i) === delimiter) {
            continue;
        }
 
        // 構文解析
         var checkString = text.charAt(i) + text.charAt(i + 1);
         if (inDoubleQuote && checkString === '""') {
            // ダブルクォーテーション内のダブルクォーテーション2つはエスケープされたダブルクォーテーションとして扱うので1文字スキップ
            skip = true;
        } else if (inDoubleQuote && checkString === '"' + delimiter) {
             // ダブルクォーテーション範囲から抜ける
             // ダブルクォーテーション範囲から抜ける
             if (inDoubleQuote && checkString1 === '"' + delimiter) {
             inDoubleQuote = false;
                inDoubleQuote = false;
        } else if (text.charAt(i) === delimiter) {
            }
             // ダブルクォーテーション範囲内のデリミタはカウントしない
             // ダブルクォーテーション範囲内のデリミタはカウントしない
             if (!inDoubleQuote) {
             if (!inDoubleQuote) {
256行目: 288行目:
             }
             }
             // ダブルクォーテーション範囲に入る
             // ダブルクォーテーション範囲に入る
             if (checkString2 === delimiter + '"') {
             if (!inDoubleQuote && checkString === delimiter + '"') {
                 inDoubleQuote = true;
                 inDoubleQuote = true;
                skip = true;
             }
             }
         } else if (inDoubleQuote && text.charAt(i) === '"' && (text.charAt(i + 1) === '\n' || text.charAt(i + 1) === '')) {
         } else if (inDoubleQuote && (checkString === '"\n' || checkString === '"')) {
             inDoubleQuote = false;
             inDoubleQuote = false;
         } else if (!inDoubleQuote && text.charAt(i) === '"' && (text.charAt(i - 1) === '\n' || i == 0)) {
         } else if (!inDoubleQuote && text.charAt(i) === '"' && (text.charAt(i - 1) === '\n' || i == 0)) {
スポンサーリンク