示例#1
0
        /// <summary>
        /// Support UTF8
        /// Create list files
        /// </summary>
        /// <param name="zip">Zip.</param>
        /// <param name="zipPath">Zip path.</param>
        /// <param name="srcDir">Source dir.</param>
        /// <param name="srcFiles">Source files.</param>
        /// <param name="deleteSrcFiles">If set to <c>true</c> delete source files.</param>
        /// <param name="removedFiles">remove files are added to the list file</param>
        public static void CreateZipAndList(this FastZip zip, string zipPath, string srcDir, IEnumerable <FileInfo> srcFiles, bool deleteSrcFiles, IEnumerable <string> removedFiles = null)
        {
            string zipName = PathUtil.GetFileNameWithoutExt(zipPath) + ".zip";
            // Compress
            List <string> paths = new List <string>();

            foreach (FileInfo f in srcFiles)
            {
                paths.Add(f.FullName);
            }

            // Generate MD5 digest for assets
            StringBuilder zipFilter   = new StringBuilder();
            StringBuilder fileListStr = new StringBuilder();

            fileListStr.Append(DownloadList.ZIP_HEADER).Append(zipName).Append("\n");

            // create zip filter and file list
            foreach (FileInfo f in srcFiles)
            {
                string fullPath     = f.FullName;
                string relativePath = PathUtil.GetRelativePath(fullPath, srcDir);
                zipFilter.Append(relativePath).Append("$;");
                fileListStr.Append(DownloadList.ZIP_ENTRY_HEADER);
                AddDigest(fileListStr, relativePath, fullPath);
            }
            // add remove list
            foreach (string r in removedFiles)
            {
                fileListStr.Append(DownloadList.DELETE_HEADER).Append(r).Append("\n");
            }
            zipFilter.Replace(@"\", @"\\");

            // Zipping
            TextReplacer replace = new TextReplacer();

            replace.AddReplaceToken(@"\(", @"\(");
            replace.AddReplaceToken(@"\)", @"\)");
            replace.AddReplaceToken(@"\+", @"\+");
            replace.AddReplaceToken(@"\.", @"\.");
            ZipEntryFactory ef = zip.EntryFactory as ZipEntryFactory;

            ef.IsUnicodeText = true;
            string filter = replace.Replace(zipFilter.ToString());

            UnityEngine.Debug.Log("srcdir:" + srcDir);
            UnityEngine.Debug.Log("filter:" + filter);
            zip.CreateZip(zipPath, srcDir, true, filter);
            if (deleteSrcFiles)
            {
                foreach (string deletePath in paths)
                {
                    File.Delete(PathUtil.Combine(PathUtil.Combine(srcDir, deletePath)));
                }
            }
            File.WriteAllText(PathUtil.ReplaceExtension(zipPath, ".bytes"), fileListStr.ToString());
        }
示例#2
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Restart();
            var txt = new TextFile(@"C:\Temp\B1.html");
            var str = txt.ReadText();

            Console.WriteLine("new txt, read" + sw.ElapsedTicks);

            sw.Restart();
            var s = new TextReplacer(str);

            Console.WriteLine("new TextReplacer" + sw.ElapsedTicks);

            sw.Restart();
            var pat   = "<span(.+?)</span>";    //"#bookmark(\\d+?\")(.+?)bookmark\\1";//"<span(.+?)</span>";
            var regex = new RegexProcessor(pat);
            var rep   = new Replacement(@"\1"); //(@"#b\1\2b\1");//(@"12345\1=234567");

            Console.WriteLine("new regex/repl" + sw.ElapsedTicks);

            sw.Restart();
            var matches = regex.RelatedMatches(str, s);

            Console.WriteLine("matches" + sw.ElapsedTicks + " in ms:" + sw.Elapsed);

            sw.Reset();
            var sw1 = new Stopwatch();

            foreach (var match in matches)
            {
                sw.Start();
                var repls = rep.CreateCopyWithGroups(match);
                sw.Stop();

                sw1.Start();
                s.Replace(match, repls);
                sw1.Stop();
            }
            Console.WriteLine("replacement build " + sw.ElapsedTicks);
            Console.WriteLine("text replace" + sw1.ElapsedTicks);

            sw.Restart();
            var res = s.BuildResult();

            sw.Stop();
            Console.WriteLine("сборка" + sw.ElapsedTicks);
            Console.ReadLine();
        }
        public void TestReplace_ByContext()
        {
            var replacer = new TextReplacer();

            var context = new DataContext();

            context.DataDictionary.AddValue("key", "dictionary");

            var source   = "Text line with key.\r\nAnother key line.\r\n";
            var expected = "Text line with dictionary.\r\nAnother dictionary line.\r\n";

            source = replacer.Replace(source, context);

            Assert.Equal(expected, source);
        }
示例#4
0
        private string ReplaceSerialized(string text)
        {
            return(_strRegex.Replace(text, match =>
            {
                string value = match.Groups[1].Value;

                string newValue = _replacer.Replace(value);
                if (newValue != value)
                {
                    Count++;
                }

                return $"s:{newValue.Length}:\\\"{newValue}\\\"" +
                match.Groups[2].Value;
            }));
        }
        public void TestReplace_ByParameters()
        {
            var replacer = new TextReplacer();

            replacer.ReplaceKeys.Add(new ReplaceParameter("key", "val"));

            var context = new DataContext();

            context.DataDictionary.AddValue("key", "dictionary");

            var source   = "Text line with key.\r\nAnother key line.\r\n";
            var expected = "Text line with val.\r\nAnother val line.\r\n";

            source = replacer.Replace(source, context);

            Assert.Equal(expected, source);
        }
示例#6
0
        public void Run(CancellationToken cancellationToken)
        {
            lock (_busy)
            {
                if (Immutable) return;

                if (_state != MicroTaskStates.None) _log.Error("_state != MicroTaskStates.None");

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Reading;
                var text = _file.ReadText();
                var replacer = new TextReplacer(text);

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Searching;
                var matches = new List<RelatedMatch>();
                var m = _regex.RelatedMatch(text, 0, replacer);
                while (m.Success)
                {
                    CurrentStagePercentage = (double)(m.StartIndex + m.Length) / text.Length;
                    matches.Add(m);
                    if (cancellationToken.IsCancellationRequested) return;
                    m = _regex.RelatedMatch(text, m.StartIndex + m.Length, replacer);
                }
                if (matches.Count == 0) matches.Add(m);

                if (!matches[0].Success)
                {
                    Immutable = true;
                    State = MicroTaskStates.Complete;
                    return;
                }

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Replacing;
                foreach (var relatedMatch in matches)
                    replacer.Replace(relatedMatch, _replacement.CreateCopyWithGroups(relatedMatch));

                State = MicroTaskStates.BuildingResult;
                var result = replacer.BuildResult();
                ReplacesCount += matches.Count;

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.SavingResult;
                _file.WriteText(result);

                State = MicroTaskStates.Complete;
            }
        }
示例#7
0
        public void Run(CancellationToken cancellationToken)
        {
            lock (_busy)
            {
                if (Immutable)
                {
                    return;
                }

                if (_state != MicroTaskStates.None)
                {
                    _log.Error("_state != MicroTaskStates.None");
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.Reading;
                var text     = _file.ReadText();
                var replacer = new TextReplacer(text);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.Searching;
                var matches = new List <RelatedMatch>();
                var m       = _regex.RelatedMatch(text, 0, replacer);
                while (m.Success)
                {
                    CurrentStagePercentage = (double)(m.StartIndex + m.Length) / text.Length;
                    matches.Add(m);
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    m = _regex.RelatedMatch(text, m.StartIndex + m.Length, replacer);
                }
                if (matches.Count == 0)
                {
                    matches.Add(m);
                }

                if (!matches[0].Success)
                {
                    Immutable = true;
                    State     = MicroTaskStates.Complete;
                    return;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.Replacing;
                foreach (var relatedMatch in matches)
                {
                    replacer.Replace(relatedMatch, _replacement.CreateCopyWithGroups(relatedMatch));
                }

                State = MicroTaskStates.BuildingResult;
                var result = replacer.BuildResult();
                ReplacesCount += matches.Count;

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.SavingResult;
                _file.WriteText(result);

                State = MicroTaskStates.Complete;
            }
        }