fan-1.0 /src/sys/dotnet/fan/sys/Version.cs

Language C# Lines 185
MD5 Hash af3cf225a80bc3a789c9e3b6141848c9 Estimated Cost $3,351 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   14 Jan 06  Andy Frank  Creation
//

using System.Text;

namespace Fan.Sys
{
  /// <summary>
  /// Version.
  /// </summary>
  public sealed class Version : FanObj
  {

  //////////////////////////////////////////////////////////////////////////
  // Construction
  //////////////////////////////////////////////////////////////////////////

    public static Version fromStr(string s) { return fromStr(s, true); }
    public static Version fromStr(string s, bool check)
    {
      List segments = new List(Sys.IntType, 4);
      int seg = -1;
      bool valid = true;
      int len = s.Length;
      for (int i=0; i<len; ++i)
      {
        int c = s[i];
        if (c == '.')
        {
          if (seg < 0 || i+1>=len) { valid = false; break; }
          segments.add(Long.valueOf(seg));
          seg = -1;
        }
        else
        {
          if ('0' <= c && c <= '9')
          {
            if (seg < 0) seg = c-'0';
            else seg = seg*10 + (c-'0');
          }
          else
          {
            valid = false; break;
          }
        }
      }
      if (seg >= 0) segments.add(Long.valueOf(seg));

      if (!valid || segments.sz() == 0)
      {
        if (check)
          throw ParseErr.make("Version", s).val;
        else
          return null;
      }

      return new Version(segments);
    }

    public static Version make(List segments)
    {
      bool valid = segments.sz() > 0;
      for (int i=0; i<segments.sz(); i++)
        if (((Long)segments.get(i)).longValue() < 0) valid = false;
      if (!valid) throw ArgErr.make("Invalid Version: '" + segments + "'").val;
      return new Version(segments);
    }

    internal Version(List segments)
    {
      this.m_segments = segments.ro();
    }

  //////////////////////////////////////////////////////////////////////////
  // Identity
  //////////////////////////////////////////////////////////////////////////

    public override bool Equals(object obj)
    {
      if (obj is Version)
        return toStr() == ((Version)obj).toStr();
      else
        return false;
    }

    public override long compare(object obj)
    {
      Version that = (Version)obj;
      List a = this.m_segments;
      List b = that.m_segments;
      for (int i=0; i<a.sz() && i<b.sz(); i++)
      {
        long ai = (a.get(i) as Long).longValue();
        long bi = (b.get(i) as Long).longValue();
        if (ai < bi) return -1;
        if (ai > bi) return +1;
      }
      if (a.sz() < b.sz()) return -1;
      if (a.sz() > b.sz()) return +1;
      return 0;
    }

    public override int GetHashCode()
    {
      return toStr().GetHashCode();
    }

    public override long hash()
    {
      return FanStr.hash(toStr());
    }

    public override Type @typeof()
    {
      return Sys.VersionType;
    }

    public override string toStr()
    {
      if (m_str == null)
      {
        StringBuilder s = new StringBuilder();
        for (int i=0; i<m_segments.sz(); i++)
        {
          if (i > 0) s.Append('.');
          s.Append(m_segments.get(i));
        }
        m_str = s.ToString();
      }
      return m_str;
    }

  //////////////////////////////////////////////////////////////////////////
  // Methods
  //////////////////////////////////////////////////////////////////////////

    public List segments()
    {
      return m_segments;
    }

    public int segment(int index)
    {
      return ((Long)m_segments.get(index)).intValue();
    }

    public long major()
    {
      return ((Long)m_segments.get(0)).longValue();
    }

    public Long minor()
    {
      if (m_segments.sz() < 2) return null;
      return (Long)m_segments.get(1);
    }

    public Long build()
    {
      if (m_segments.sz() < 3) return null;
      return (Long)m_segments.get(2);
    }

    public Long patch()
    {
      if (m_segments.sz() < 4) return null;
      return (Long)m_segments.get(3);
    }

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    public static readonly Version m_defVal = fromStr("0");

    private readonly List m_segments;
    private string m_str;

  }
}
Back to Top