/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/RegexFlags.java

https://github.com/spring-projects/spring-data-mongodb · Java · 116 lines · 51 code · 23 blank · 42 comment · 11 complexity · f6f4a75e066d954bef0616e6b4737400 MD5 · raw file

  1. /*
  2. * Copyright 2021-2022 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.mongodb.util;
  17. import java.util.regex.Pattern;
  18. import org.springframework.lang.Nullable;
  19. /**
  20. * Utility to translate {@link Pattern#flags() regex flags} to MongoDB regex options and vice versa.
  21. *
  22. * @author Mark Paluch
  23. * @since 3.3
  24. */
  25. public abstract class RegexFlags {
  26. private static final int[] FLAG_LOOKUP = new int[Character.MAX_VALUE];
  27. static {
  28. FLAG_LOOKUP['g'] = 256;
  29. FLAG_LOOKUP['i'] = Pattern.CASE_INSENSITIVE;
  30. FLAG_LOOKUP['m'] = Pattern.MULTILINE;
  31. FLAG_LOOKUP['s'] = Pattern.DOTALL;
  32. FLAG_LOOKUP['c'] = Pattern.CANON_EQ;
  33. FLAG_LOOKUP['x'] = Pattern.COMMENTS;
  34. FLAG_LOOKUP['d'] = Pattern.UNIX_LINES;
  35. FLAG_LOOKUP['t'] = Pattern.LITERAL;
  36. FLAG_LOOKUP['u'] = Pattern.UNICODE_CASE;
  37. }
  38. private RegexFlags() {
  39. }
  40. /**
  41. * Lookup the MongoDB specific options from given {@link Pattern#flags() flags}.
  42. *
  43. * @param flags the Regex flags to look up.
  44. * @return the options string. May be empty.
  45. */
  46. public static String toRegexOptions(int flags) {
  47. if (flags == 0) {
  48. return "";
  49. }
  50. StringBuilder buf = new StringBuilder();
  51. for (int i = 'a'; i < 'z'; i++) {
  52. if (FLAG_LOOKUP[i] == 0) {
  53. continue;
  54. }
  55. if ((flags & FLAG_LOOKUP[i]) > 0) {
  56. buf.append((char) i);
  57. }
  58. }
  59. return buf.toString();
  60. }
  61. /**
  62. * Lookup the MongoDB specific flags for a given regex option string.
  63. *
  64. * @param s the Regex option/flag to look up. Can be {@literal null}.
  65. * @return zero if given {@link String} is {@literal null} or empty.
  66. * @since 2.2
  67. */
  68. public static int toRegexFlags(@Nullable String s) {
  69. int flags = 0;
  70. if (s == null) {
  71. return flags;
  72. }
  73. for (char f : s.toLowerCase().toCharArray()) {
  74. flags |= toRegexFlag(f);
  75. }
  76. return flags;
  77. }
  78. /**
  79. * Lookup the MongoDB specific flags for a given character.
  80. *
  81. * @param c the Regex option/flag to look up.
  82. * @return
  83. * @throws IllegalArgumentException for unknown flags
  84. * @since 2.2
  85. */
  86. public static int toRegexFlag(char c) {
  87. int flag = FLAG_LOOKUP[c];
  88. if (flag == 0) {
  89. throw new IllegalArgumentException(String.format("Unrecognized flag [%c]", c));
  90. }
  91. return flag;
  92. }
  93. }