示例#1
0
        public static DateInterval __construct(Env env, string time)
        {
            DateInterval dateInterval = new DateInterval();

            IntervalParser parser = new IntervalParser(dateInterval, time);

            parser.parse();

            return(dateInterval);
        }
示例#2
0
        public void Test(string token, IntervalType expectedIntervalType)
        {
            // Arrange
            IntervalParser intervalParser = new IntervalParser();

            // Act
            IntervalType actualIntervalType = intervalParser.Parse(token);

            // Assert
            Assert.Equal(expectedIntervalType, actualIntervalType);
        }
示例#3
0
 public void CanParseIntervals(string str, TimeSpan?expected)
 {
     if (str == null)
     {
         Assert.Throws <ArgumentNullException>(() => IntervalParser.Parse(str));
     }
     else if (expected == null)
     {
         Assert.Throws <ArgumentException>(() => IntervalParser.Parse(str));
     }
     else
     {
         Assert.Equal(expected, IntervalParser.Parse(str));
     }
 }
示例#4
0
        public void Parse_ValidParse_ResultEqualsExpected <TSet, TStruct, TParser> (
            String inputString,
            TSet hack1,
            TStruct hack2,
            TParser hack3,
            IntervalParser <TSet, TStruct, TParser> parser,
            Interval <TSet, TStruct> expected)
            where TSet : IComparable
            where TStruct : IStructure, new()
            where TParser : IParser <TSet>, new()
        {
            var result = parser.Parse(inputString);

            Assert.IsNotNull(result);

            Assert.AreEqual(expected, result);
        }
示例#5
0
        public static IServiceCollection AddWhaleCache(this IServiceCollection services, ServiceConfig config, ILogger logger)
        {
            logger?.LogInformation($"Default object cache lifetime: {config.CacheTtl}");

            if (string.IsNullOrEmpty(config.RedisCache))
            {
                logger?.LogInformation("Using in-memory cache.");
                services.AddSingleton <IMemoryCache>(new MemoryCache(new MemoryCacheOptions {
                }));
                services.AddSingleton <ICacheFactory>(provider => new MemCacheFactory(provider.GetService <IMemoryCache>())
                {
                    Ttl = IntervalParser.Parse(config.CacheTtl)
                });
            }
            else
            {
                logger?.LogInformation($"Using Redis cache ({config.RedisCache})");
                var ready     = false;
                var retryTime = 15;
                while (!ready)
                {
                    try
                    {
                        services.AddSingleton <IConnectionMultiplexer>(ConnectionMultiplexer.Connect(config.RedisCache));
                        ready = true;
                    }
                    catch (Exception ex)
                    {
                        logger?.LogWarning($"Could not connect to redis instance. Retrying in {retryTime}s.");
                        logger?.LogInformation($"Redis connection string: {config.RedisCache}");
                        logger?.LogError(ex, "Redis connection error");
                        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(retryTime));
                    }
                }

                services.AddSingleton <ICacheFactory>(p => new RedCacheFactory {
                    Mux = p.GetService <IConnectionMultiplexer>(), Ttl = IntervalParser.Parse(config.CacheTtl)
                });
            }

            return(services);
        }
示例#6
0
 public BitArray Parse() {
     BitArray result = new IntervalParser(_ranges[0]).Parse();
     for (int i = 1; i < _ranges.Length; i++) {
         result.And(new IntervalParser(_ranges[i]).Parse());
     }
     return result;
 }
示例#7
0
        private static MutableString/*!*/ TrInternal(MutableString/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ from,
            [DefaultProtocol, NotNull]MutableString/*!*/ to, bool squeeze) {

            MutableString result = self.CreateInstance().TaintBy(self);
            IntervalParser parser = new IntervalParser(from);

            // TODO: a single pass to generate both?
            MutableString source = parser.ParseSequence();
            BitArray bitmap = parser.Parse();

            MutableString dest = new IntervalParser(to).ParseSequence();

            int lastChar = dest.GetLastChar();
            char? lastTranslated = null;
            for (int i = 0; i < self.Length; i++) {
                char c = self.GetChar(i);
                if (bitmap.Get(c)) {
                    char? thisTranslated = null;
                    int index = source.IndexOf(c);
                    if (index >= dest.Length) {
                        if (lastChar != -1) {
                            thisTranslated = (char)lastChar;
                        }
                    } else {
                        thisTranslated = dest.GetChar(index);
                    }
                    if (thisTranslated != null && (!squeeze || lastTranslated == null || lastTranslated.Value != thisTranslated)) {
                        result.Append(thisTranslated.Value);
                    }
                    lastTranslated = thisTranslated;
                } else {
                    result.Append(c);
                    lastTranslated = null;
                }
            }

            return result;
        }
示例#8
0
 public void CanTryParseIntervals(string str, TimeSpan?expected)
 {
     Assert.Equal(expected != null, IntervalParser.TryParse(str, out var timeSpan));
     Assert.Equal(expected ?? default, timeSpan);
 }