суббота, 13 января 2018 г.

Best way to reverse a string

https://stackoverflow.com/questions/228038/best-way-to-reverse-a-string


public static string ReverseXor(string s)
        {
            char[] charArray = s.ToCharArray();
            int len = s.Length - 1;

            for (int i = 0; i < len; i++, len--)
            {
                charArray[i] ^= charArray[len];
                charArray[len] ^= charArray[i];
                charArray[i] ^= charArray[len];
            }

            return new string(charArray);
        }

        public static string ReverseSB(string text)
        {
            StringBuilder builder = new StringBuilder(text.Length);
            for (int i = text.Length - 1; i >= 0; i--)
            {
                builder.Append(text[i]);
            }
            return builder.ToString();
        }

        public static string ReverseArray(string text)
        {
            char[] array = text.ToCharArray();
            Array.Reverse(array);
            return (new string(array));
        }

26251 Ticks String Builder (Length: 1) : called 10000 times.
33373 Ticks Array.Reverse (Length: 1) : called 10000 times.
20162 Ticks Xor (Length: 1) : called 10000 times.

51321 Ticks String Builder (Length: 10) : called 10000 times.
37105 Ticks Array.Reverse (Length: 10) : called 10000 times.
23974 Ticks Xor (Length: 10) : called 10000 times.

66570 Ticks String Builder (Length: 15) : called 10000 times.
26027 Ticks Array.Reverse (Length: 15) : called 10000 times.
24017 Ticks Xor (Length: 15) : called 10000 times.

101609 Ticks String Builder (Length: 25) : called 10000 times.
28472 Ticks Array.Reverse (Length: 25) : called 10000 times.
35355 Ticks Xor (Length: 25) : called 10000 times.

161601 Ticks String Builder (Length: 50) : called 10000 times.
35839 Ticks Array.Reverse (Length: 50) : called 10000 times.
51185 Ticks Xor (Length: 50) : called 10000 times.

230898 Ticks String Builder (Length: 75) : called 10000 times.
40628 Ticks Array.Reverse (Length: 75) : called 10000 times.
78906 Ticks Xor (Length: 75) : called 10000 times.

312017 Ticks String Builder (Length: 100) : called 10000 times.
52225 Ticks Array.Reverse (Length: 100) : called 10000 times.
110195 Ticks Xor (Length: 100) : called 10000 times.

2970691 Ticks String Builder (Length: 1000) : called 10000 times.
292094 Ticks Array.Reverse (Length: 1000) : called 10000 times.
846585 Ticks Xor (Length: 1000) : called 10000 times.

305564115 Ticks String Builder (Length: 100000) : called 10000 times.
74884495 Ticks Array.Reverse (Length: 100000) : called 10000 times.
125409674 Ticks Xor (Length: 100000) : called 10000 times.
It seems that Xor can be faster for short strings.

четверг, 11 января 2018 г.

FTP UpLoad File

https://stackoverflow.com/questions/3576436/upload-a-file-with-encoding-using-ftp-in-c-sharp

private static void up(string sourceFile, string targetFile)
{            
    try
    {
        string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"];
        string ftpUserID = ConfigurationManager.AppSettings["ftpUser"];
        string ftpPassword = ConfigurationManager.AppSettings["ftpPass"];
        ////string ftpURI = "";
        string filename = "ftp://" + ftpServerIP + "//" + targetFile; 
        FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
        ftpReq.UseBinary = true;
        ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
        ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

        byte[] b = File.ReadAllBytes(sourceFile);

        ftpReq.ContentLength = b.Length;
        using (Stream s = ftpReq.GetRequestStream())
        {
            s.Write(b, 0, b.Length);
        }

        FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

        if (ftpResp != null)
        {
            MessageBox.Show(ftpResp.StatusDescription);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Character Encoding during FTP transfer

http://support.2brightsparks.com/knowledgebase/articles/726108-character-encoding-during-ftp-transfer

While using FTP transfers, SyncBack users sometimes encounter problem with misrepresentation of non-English characters in file names such as umlauts (a mark used over a vowel), accented letters, Chinese or Arabic characters.  
FTP is an old protocol (first designed in 1971 with the current protocols from 1985) and the support for non-English characters in file names were not considered when the protocol was initially designed. Therefore, it was designed to be used with English characters only (7-bit ASCII). Later the protocol was extended to use UTF-8 as the character set to support non-English characters in file names.   
SyncBack is fully compliant with the extended specifications and uses UTF-8 (Unicode) if the FTP server supports it. However, if you experience problems with FTP transfer using SyncBack, such as copying file names containing non-English characters to your FTP server or file names are shown with wrong characters after they are transferred to your FTP server, then it could be due to one or more of the following reasons:
  1. Your FTP server follows the original specifications and supports English characters only and rejects those file names with foreign characters. 
  2. Your FTP server violates the specifications and uses custom encoding that SyncBack does not understand.
  3. If your FTP server states that it is Unicode compatible, then SyncBack will use UTF-8 to transfer files. But if you notice that the file names are shown with wrong characters on your FTP server, then it is possible that the FTP client you are using to view the file names is not Unicode compatible or does not understand Unicode, and hence it displays the file names with wrong characters. Alternatively, you could use an FTP client that is Unicode compatible or download that file from your FTP server using SyncBack and verify the file names on your local drive.
Note that if your FTP Server or client (used to view the transferred files) are not Unicode compatible then please upgrade to FTP software that uses UTF-8 (for example FileZilla - https://filezilla-project.org/) or refrain from using foreign characters.

https://winscp.net/eng/docs/ui_login_environment#utf

четверг, 4 января 2018 г.

With Clause with regex

http://www.sql.ru/forum/921216/regexp-udalenie-podstroki-iz-stroki-mezhdu-dvumya-shablonami

https://dev.mysql.com/doc/refman/8.0/en/with.html

WITH cte1 AS (SELECT a, b FROM table1), cte2 AS (SELECT c, d FROM table2) SELECT b, d FROM cte1 JOIN cte2 WHERE cte1.a = cte2.c;


with s as(select '"select j.FIO \"Фамилия, Имя Отчество\", PERSON_ID \"ID человека\""' str from dual)
select str,
regexp_replace(str, '\\"[^\"]*\\"', '')
from s;
StellaMaris

SELECT a.title, a.fulltext FROM maris01.xx2sk_content a WHERE a.fulltext LIKE '%<!--58dba6d87ab6e1490790104-->%' OR a.fulltext LIKE '%<!--/58dba6d87ab6e1490790104-->%'
;