コード例 #1
0
        /// <summary>
        /// Gets the string representation of the ksuid.
        /// </summary>
        public override string ToString()
        {
            if (str != null)
            {
                return(str);
            }

            var sb = new StringBuilder();

            sb.Append(Environment == "prod" ? "" : $"{Environment}_");
            sb.Append(Resource);
            sb.Append("_");

            var decoded    = new byte[Constants.DecodedLength];
            var timestamp  = BitConverter.GetBytes(Timestamp);
            var instance   = InstanceIdentifier.ToByteArray();
            var sequenceId = BitConverter.GetBytes(SequenceId);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(timestamp);
                Array.Reverse(sequenceId);
            }

            Array.Copy(timestamp, decoded, 8);
            Array.Copy(instance, 0, decoded, 8, 9);
            Array.Copy(sequenceId, 0, decoded, 17, 4);

            sb.Append(Base62.Encode(decoded).PadLeft(Constants.EncodedLength, '0'));

            return(str = sb.ToString());
        }
コード例 #2
0
        /// <summary>
        /// Parses a string id represention of a KSUID.
        /// </summary>
        /// <param name="id">The string representation of the ksuid.</param>
        public static Id Parse(string input)
        {
            var parts   = splitPrefix(input);
            var decoded = Base62.Decode(parts.payload);

            var timestamp = new byte[8];
            var instance  = new byte[9];
            var sequence  = new byte[4];

            Array.Copy(decoded, timestamp, 8);
            Array.Copy(decoded, 8, instance, 0, 9);
            Array.Copy(decoded, 17, sequence, 0, 4);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(timestamp);
                Array.Reverse(sequence);
            }

            return(new Id(
                       parts.environment,
                       parts.resource,
                       BitConverter.ToUInt64(timestamp, 0),
                       new InstanceIdentifier(instance),
                       BitConverter.ToUInt32(sequence, 0)
                       ));
        }