You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util;
  18. import javax.annotation.Nonnull;
  19. /**
  20. * Utilities for dealing with strings.
  21. */
  22. public final class StringUtils {
  23. private StringUtils() {
  24. // Shouldn't be instansiated.
  25. }
  26. /**
  27. * Returns the indexes for the word surrounding the index in the specified string.
  28. *
  29. * @param text Text to get word from
  30. * @param index Index to get surrounding word
  31. *
  32. * @return An array containing two elements: the index of the first character of the word, and
  33. * the index of the first character beyond the end of the word. If the specified index is not
  34. * contained within a word (i.e., is whitespace) then 0,0 is returned.
  35. */
  36. public static int[] indiciesOfWord(@Nonnull final CharSequence text, final int index) {
  37. final int start = indexOfStartOfWord(text, index);
  38. final int end = indexOfEndOfWord(text, index);
  39. if (start > end) {
  40. return new int[]{0, 0};
  41. }
  42. return new int[]{start, end};
  43. }
  44. /**
  45. * Returns the start index for the word surrounding the index in the specified string.
  46. *
  47. * @param text Text to get word from
  48. * @param index Index to get surrounding word
  49. *
  50. * @return Start index of the word surrounding the index
  51. */
  52. public static int indexOfStartOfWord(@Nonnull final CharSequence text, final int index) {
  53. int start = index;
  54. // Traverse backwards
  55. while (start > 0 && start < text.length() && text.charAt(start) != ' ') {
  56. start--;
  57. }
  58. if (start + 1 < text.length() && text.charAt(start) == ' ') {
  59. start++;
  60. }
  61. return start;
  62. }
  63. /**
  64. * Returns the end index for the word surrounding the index in the specified string.
  65. *
  66. * @param text Text to get word from
  67. * @param index Index to get surrounding word
  68. *
  69. * @return End index of the word surrounding the index
  70. */
  71. public static int indexOfEndOfWord(@Nonnull final CharSequence text, final int index) {
  72. int end = index;
  73. // And forwards
  74. while (end < text.length() && end >= 0 && text.charAt(end) != ' ') {
  75. end++;
  76. }
  77. return end;
  78. }
  79. }