字符串数组中的制表符补全

发布于 2025-01-01 07:54:37 字数 1663 浏览 0 评论 0原文

我正在构建一个 IRC 客户端,我希望实现一个选项卡完整名称的解决方案。我有一个数组形式的用户列表。当用户按下 Tab 键时,它会完成用户名。当他们再次按下该键时,下一个用户完成。

我这里有一个可行的解决方案,但我觉得它可以更加优化和简洁。如果有任何建议,我将不胜感激。

// Get Active Window
var channel = irc.chatWindows.getActive();
// Get users input
var sentence = $('#chat-input').val().split(' ');
// Get the last word in the sentence
var partialMatch = sentence.pop();
// Get list of users
var users = channel.userList.getUsers();
// Set default index for loop
var userIndex=0;
//Persist the match
//Store the partialMatch to persist next time the user hits tab
if(window.partialMatch === undefined) {
  window.partialMatch = partialMatch;
} else if(partialMatch.search(window.partialMatch) !== 0){
  window.partialMatch = partialMatch;
} else {
  if (sentence.length === 0) {
    userIndex = users.indexOf(partialMatch.substr(0, partialMatch.length-1));
  } else {
    userIndex = users.indexOf(partialMatch);
  }
}
//Cycle through userlist from last user or beginning
for (var i=userIndex; i<users.length; i++) {
  var user = users[i] || '';
  //Search for match
  if (window.partialMatch.length > 0 && user.search(window.partialMatch, "i") === 0) {
    //If no match found we continue searching
    if(user === partialMatch || user === partialMatch.substr(0, partialMatch.length-1)){
      continue;
    }
    //If we find a match we return our match to our input
    sentence.push(user);
    //We decide whether or not to add colon
    if (sentence.length === 1) {
      $('#chat-input').val(sentence.join(' ') +  ":");
    } else {
      $('#chat-input').val(sentence.join(' '));
    }
    //We break from our loop
    break;
  }
}

I am building an IRC client and I am hoping to implement a solution to tab complete names. I have a list of users in the form of an array. When the user presses the tab key it completes the username. When they press the key again it completes with the next user.

I have a working solution here, but I feel like it could be a little more optimized and terse. I would be grateful for any suggestions.

// Get Active Window
var channel = irc.chatWindows.getActive();
// Get users input
var sentence = $('#chat-input').val().split(' ');
// Get the last word in the sentence
var partialMatch = sentence.pop();
// Get list of users
var users = channel.userList.getUsers();
// Set default index for loop
var userIndex=0;
//Persist the match
//Store the partialMatch to persist next time the user hits tab
if(window.partialMatch === undefined) {
  window.partialMatch = partialMatch;
} else if(partialMatch.search(window.partialMatch) !== 0){
  window.partialMatch = partialMatch;
} else {
  if (sentence.length === 0) {
    userIndex = users.indexOf(partialMatch.substr(0, partialMatch.length-1));
  } else {
    userIndex = users.indexOf(partialMatch);
  }
}
//Cycle through userlist from last user or beginning
for (var i=userIndex; i<users.length; i++) {
  var user = users[i] || '';
  //Search for match
  if (window.partialMatch.length > 0 && user.search(window.partialMatch, "i") === 0) {
    //If no match found we continue searching
    if(user === partialMatch || user === partialMatch.substr(0, partialMatch.length-1)){
      continue;
    }
    //If we find a match we return our match to our input
    sentence.push(user);
    //We decide whether or not to add colon
    if (sentence.length === 1) {
      $('#chat-input').val(sentence.join(' ') +  ":");
    } else {
      $('#chat-input').val(sentence.join(' '));
    }
    //We break from our loop
    break;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-01-08 07:54:37

您可能想研究一下 trie 数据结构,这是非常好的正是针对这个问题而构建的。使用 trie,您可以非常有效地列出以给定前缀开头的所有字符串,而无需查看单词列表中的所有单词。您还可以使用 trie 执行其他不错的操作,例如快速查找、快速后继和前驱搜索以及快速插入/删除。

希望这有帮助!

You may want to look into the trie data structure, which is excellently structured for exactly this problem. With a trie, you can list off all of the strings that start with a given prefix very efficiently and without having to look at all the words in the word list. You can also do other nice operations with the trie, such as fast lookups, fast successor and predecessor search, and fast insertion/deletion.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文