From 0ec02fd482b4b8aebe2f02a0ac94a4ec6d1e90dc Mon Sep 17 00:00:00 2001 From: irengrig Date: Thu, 9 Mar 2017 11:59:46 +0100 Subject: [PATCH] have a separate SemVerMatcher for parsing semver versions in 2 variants: strict or partial. Fixes SemVerTest --- .../src/com/intellij/util/text/SemVer.java | 32 +--------- .../com/intellij/util/text/SemVerMatcher.java | 61 +++++++++++++++++++ .../com/intellij/util/text/SemVerTest.java | 4 +- 3 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 platform/util/src/com/intellij/util/text/SemVerMatcher.java diff --git a/platform/util/src/com/intellij/util/text/SemVer.java b/platform/util/src/com/intellij/util/text/SemVer.java index 075c48a60cf1..953edb4491d0 100644 --- a/platform/util/src/com/intellij/util/text/SemVer.java +++ b/platform/util/src/com/intellij/util/text/SemVer.java @@ -15,7 +15,6 @@ */ package com.intellij.util.text; -import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -86,39 +85,10 @@ public class SemVer implements Comparable { return myRawVersion; } - @Nullable - public static SemVer parseFromText(@NotNull String text) { - int majorEndInd = text.indexOf('.'); - if (majorEndInd < 0) { - final int major = StringUtil.parseInt(text, -1); - return major < 0 ? null : new SemVer(text, major, 0, 0); - } - int major = StringUtil.parseInt(text.substring(0, majorEndInd), -1); - int minorEndInd = text.indexOf('.', majorEndInd + 1); - if (minorEndInd < 0) { - final int minor = StringUtil.parseInt(text.substring(majorEndInd + 1), -1); - return new SemVer(text, major, minor < 0 ? 0 : minor, 0); - } - int minor = StringUtil.parseInt(text.substring(majorEndInd + 1, minorEndInd), -1); - final String patchStr; - int dashInd = text.indexOf('-', minorEndInd + 1); - if (dashInd >= 0) { - patchStr = text.substring(minorEndInd + 1, dashInd); - } - else { - patchStr = text.substring(minorEndInd + 1); - } - int patch = StringUtil.parseInt(patchStr, -1); - if (major >= 0 && minor >= 0 && patch >= 0) { - return new SemVer(text, major, minor, patch); - } - return null; - } - @NotNull public static SemVer parseFromTextNonNullize(@Nullable final String text) { if (text == null) return UNKNOWN; - final SemVer ver = parseFromText(text); + final SemVer ver = SemVerMatcher.parseFromText(text); return ver == null ? UNKNOWN : ver; } diff --git a/platform/util/src/com/intellij/util/text/SemVerMatcher.java b/platform/util/src/com/intellij/util/text/SemVerMatcher.java new file mode 100644 index 000000000000..384e4dfce11e --- /dev/null +++ b/platform/util/src/com/intellij/util/text/SemVerMatcher.java @@ -0,0 +1,61 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.text; + +import com.intellij.openapi.util.text.StringUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Irina.Chernushina on 3/9/2017. + */ +public class SemVerMatcher { + @Nullable + public static SemVer parseFromText(@NotNull String text) { + return parseFromText(text, false); + } + + @Nullable + public static SemVer parseFromText(@NotNull String text, final boolean allowPartial) { + int majorEndInd = text.indexOf('.'); + if (majorEndInd < 0) { + if (!allowPartial) return null; + final int major = StringUtil.parseInt(text, -1); + return major < 0 ? null : new SemVer(text, major, 0, 0); + } + int major = StringUtil.parseInt(text.substring(0, majorEndInd), -1); + int minorEndInd = text.indexOf('.', majorEndInd + 1); + if (minorEndInd < 0) { + if (!allowPartial) return null; + final int minor = StringUtil.parseInt(text.substring(majorEndInd + 1), -1); + return new SemVer(text, major, minor < 0 ? 0 : minor, 0); + } + int minor = StringUtil.parseInt(text.substring(majorEndInd + 1, minorEndInd), -1); + final String patchStr; + int dashInd = text.indexOf('-', minorEndInd + 1); + if (dashInd >= 0) { + patchStr = text.substring(minorEndInd + 1, dashInd); + } + else { + patchStr = text.substring(minorEndInd + 1); + } + int patch = StringUtil.parseInt(patchStr, -1); + if (major >= 0 && minor >= 0 && patch >= 0) { + return new SemVer(text, major, minor, patch); + } + return null; + } +} diff --git a/platform/util/testSrc/com/intellij/util/text/SemVerTest.java b/platform/util/testSrc/com/intellij/util/text/SemVerTest.java index 00f39f14aa73..00bdd74e16e2 100644 --- a/platform/util/testSrc/com/intellij/util/text/SemVerTest.java +++ b/platform/util/testSrc/com/intellij/util/text/SemVerTest.java @@ -47,7 +47,7 @@ public class SemVerTest extends TestCase { } private static void checkNotParsed(@NotNull String version) { - assertNull(SemVer.parseFromText(version)); + assertNull(SemVerMatcher.parseFromText(version)); } public void testCompare() throws Exception { @@ -65,7 +65,7 @@ public class SemVerTest extends TestCase { @NotNull private static SemVer parseNotNull(@NotNull String text) { - SemVer semVer = SemVer.parseFromText(text); + SemVer semVer = SemVerMatcher.parseFromText(text); assertNotNull(semVer); return semVer; }