bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / compareversionstrings.cxx
blobcc8cb440733ef69d8fbaafec993124eef2f80f86
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <ctype.h>
11 #include <ctype.h>
12 #include <string.h>
14 #include <comphelper/string.hxx>
15 #include <rtl/ustring.hxx>
17 namespace comphelper { namespace string {
19 namespace {
21 // BSD licensed, from http://git.musl-libc.org/cgit/musl/plain/src/string/strverscmp.c
23 int strverscmp(const char *l, const char *r)
25 int haszero=1;
26 while (*l==*r) {
27 if (!*l) return 0;
29 if (*l=='0') {
30 if (haszero==1) {
31 haszero=0;
33 } else if (isdigit(*l)) {
34 if (haszero==1) {
35 haszero=2;
37 } else {
38 haszero=1;
40 l++; r++;
42 if (haszero==1 && (*l=='0' || *r=='0')) {
43 haszero=0;
45 if ((isdigit(*l) && isdigit(*r) ) && haszero) {
46 size_t lenl=0, lenr=0;
47 while (isdigit(l[lenl]) ) lenl++;
48 while (isdigit(r[lenr]) ) lenr++;
49 if (lenl==lenr) {
50 return (*l - *r);
51 } else if (lenl>lenr) {
52 return 1;
53 } else {
54 return -1;
56 } else {
57 return (*l - *r);
61 } // anonymous namespace
63 int compareVersionStrings(const OUString& a, const OUString& b)
65 return strverscmp(OUStringToOString(a, RTL_TEXTENCODING_UTF8).getStr(), OUStringToOString(b, RTL_TEXTENCODING_UTF8).getStr());
68 } }
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */