-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1353 from EvilBeaver/feature/issue-1327-adjustvalue
Приведение значения составного типа для 2.0
- Loading branch information
Showing
4 changed files
with
544 additions
and
193 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/OneScript.StandardLibrary/TypeDescriptions/TypeComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*---------------------------------------------------------- | ||
This Source Code Form is subject to the terms of the | ||
Mozilla Public License, v.2.0. If a copy of the MPL | ||
was not distributed with this file, You can obtain one | ||
at http://mozilla.org/MPL/2.0/. | ||
----------------------------------------------------------*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using OneScript.Types; | ||
using OneScript.Values; | ||
|
||
namespace OneScript.StandardLibrary.TypeDescriptions | ||
{ | ||
internal class TypeComparer : IComparer<BslTypeValue> | ||
{ | ||
private const string TYPE_BINARYDATA_NAME = "ДвоичныеДанные"; | ||
private static readonly IDictionary<TypeDescriptor, int> primitives = new Dictionary<TypeDescriptor, int>(); | ||
|
||
public int Compare(BslTypeValue x, BslTypeValue y) | ||
{ | ||
if (x.TypeValue.Equals(y)) return 0; | ||
|
||
var primitiveX = PrimitiveIndex(x); | ||
var primitiveY = PrimitiveIndex(y); | ||
|
||
if (primitiveX != -1) | ||
{ | ||
if (primitiveY != -1) | ||
return primitiveX - primitiveY; | ||
|
||
return -1; | ||
} | ||
|
||
if (primitiveY != -1) | ||
return 1; | ||
|
||
return x.TypeValue.Id.CompareTo(y.TypeValue.Id); | ||
} | ||
|
||
private int PrimitiveIndex(BslTypeValue type) | ||
{ | ||
if (StringComparer.CurrentCultureIgnoreCase.Equals(type.TypeValue.Name, TYPE_BINARYDATA_NAME)) | ||
{ | ||
// Пора двоичным данным стать примитивом | ||
return 1; | ||
} | ||
|
||
if (primitives.TryGetValue(type.TypeValue, out var index)) | ||
return index; | ||
|
||
return -1; | ||
} | ||
|
||
static TypeComparer() | ||
{ | ||
primitives.Add(BasicTypes.Boolean, 0); | ||
primitives.Add(BasicTypes.String, 2); | ||
primitives.Add(BasicTypes.Date, 3); | ||
primitives.Add(BasicTypes.Null, 4); | ||
primitives.Add(BasicTypes.Number, 5); | ||
primitives.Add(BasicTypes.Type, 6); | ||
} | ||
|
||
} | ||
} | ||
|
Oops, something went wrong.