You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.8 KiB

// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name flags.go
// Created by DEV
// Modified 29/01/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package js
import (
"strings"
"time"
)
const (
FlagOmitEmpty Flags = 1 << iota
FlagQuote
FlagHTML
FlagBinary
FlagDateRFC822 Flags = 0b0001000000000000
FlagDateUnix Flags = 0b0010000000000000
FlagDateANSIC Flags = 0b0100000000000000
FlagDateRFC850 Flags = 0b1000000000000000
dateMask = 0b1111000000000000
flagMask Flags = 0b0000111111111111
)
type Flags uint16
func (f Flags) OmitEmpty () bool {
return f&FlagOmitEmpty != 0
}
func (f Flags) Quote () bool {
return f&FlagQuote != 0
}
func (f Flags) EscapeHTML () bool {
return f&FlagHTML != 0
}
func (f Flags) Binary () bool {
return f&FlagBinary != 0
}
func (f Flags) SetWithName(flag string) Flags {
switch strings.ToLower(flag) {
case "o","omitempty": return f | FlagOmitEmpty
case "q","quote" : return f | FlagQuote
case "b","binary" : return f | FlagBinary
case "h","html" : return f | FlagHTML
// formatos de fechas
case "unix" : return (f & flagMask) | FlagDateUnix
case "rfc822" : return (f & flagMask) | FlagDateRFC822
case "ansic" : return (f & flagMask) | FlagDateANSIC
case "rfc850" : return (f & flagMask) | FlagDateRFC850
case "rfc822z": return f & flagMask
default: return f
}
}
func (f Flags) SetWithNames(flags ... string) Flags {
nf := f
for _,n := range flags {
switch strings.ToLower(n) {
case "o","omitempty": nf |= FlagOmitEmpty
case "q","quote" : nf |= FlagQuote
case "b","binary" : nf |= FlagBinary
case "h","html" : nf |= FlagHTML
// formatos de fechas
case "unix" : nf = (nf & flagMask) | FlagDateUnix
case "rfc822" : nf = (nf & flagMask) | FlagDateRFC822
case "ansic" : nf = (nf & flagMask) | FlagDateANSIC
case "rfc850" : nf = (nf & flagMask) | FlagDateRFC850
case "rfc822z": nf &= flagMask
}
}
return nf
}
func (f Flags) SetWithString (flags string) Flags {
if len(flags) == 0 {
return f
}
nf := f
for flags != "" {
var op string
op,flags,_ = strings.Cut(flags,",")
nf |= f.SetWithName(op)
}
return nf
}
func (f Flags) Names () string {
var ls []string
if f&FlagOmitEmpty != 0 { ls = append(ls,"omitempty")}
if f&FlagQuote != 0 { ls = append(ls,"quote")}
if f&FlagHTML != 0 { ls = append(ls,"html")}
if f&FlagBinary != 0 { ls = append(ls,"binary")}
if f&dateMask != 0 {
ls = append(ls,f.DateName())
}
if len(ls) > 0 {
return strings.Join(ls,",")
}
return ""
}
func (f Flags) DateName () string {
switch f & dateMask {
case FlagDateUnix : return "unix"
case FlagDateRFC822: return "rfc822"
case FlagDateANSIC : return "ansic"
case FlagDateRFC850: return "rfc8550"
default: return time.RFC822Z
}
}
func (f Flags) DateFormat () string {
switch f & dateMask {
case FlagDateUnix : return time.UnixDate
case FlagDateRFC822: return time.RFC822
case FlagDateANSIC : return time.ANSIC
case FlagDateRFC850: return time.RFC850
default: return time.RFC822Z
}
}

Powered by TurnKey Linux.